Skip to Content

Automating Jobs using AWS Lambda: Real Use Cases & Python Examples

As tech teams grow, simple backend tasks like sending out reminders, tidying up logs, syncing data, resizing images, or grabbing updates from third-party APIs, generate orders or sending some reports can easily turn into little roadblocks.

Traditionally, we might:

  • Run a cron job on a server
  • Deploy a microservice to handle the task
  • Build queues with workers, just for a small function

That’s overkill in many cases.

We needed a cost-effective, event-driven, zero-maintenance solution. Enter AWS Lambda.

In this post, I’ll walk you through:

  • Why and when to use Lambda
  • How I automated a real task using Lambda + Python
  • A step-by-step deployment with actual working code

What is AWS Lambda?

AWS Lambda is a serverless compute service that runs your code in response to events — without you provisioning or managing servers. You upload code, set a trigger, and it runs on-demand.

Highlights:

  • Pay per execution
  • Auto-scaling and concurrency
  • Event-driven: API Gateway, S3, CloudWatch, DynamoDB, etc.

Here is an use case of it.

An external IP lookup — for serverless, it’s easiest to use requests + a free API like ipinfo.io or ip-api.com.

Note: Lambda can’t use requests by default — so you’ll need to bundle it if you use it. Or just use Python’s built-in urllib.

import json
import urllib.request

def lambda_handler(event, context):
    apikey = 'my-demo-key'
    # Support both API Gateway & direct test
    if 'body' in event:
        body = json.loads(event['body'])
    else:
        body = event

    ip = body.get('ip')
    key = body.get('apikey')
    if not ip:
        return {
            'statusCode': 400,
            'body': json.dumps({'error': 'Missing "ip" parameter'})
        }

    if not key:
        return {
            'statusCode': 400,
            'body': json.dumps({'error': 'Missing "key" parameter'})
        }
   if key != apikey:
        return {
            'statusCode': 400,
            'body': json.dumps({'error': 'Invalid "key" '})
        }

    # Call free IP API
    url = f'http://ip-api.com/json/{ip}'

    try:
        with urllib.request.urlopen(url) as response:
            data = json.loads(response.read().decode())

        return {
            'statusCode': 200,
            'body': json.dumps(data)
        }

    except Exception as e:
        return {
            'statusCode': 500,
            'body': json.dumps({'error': str(e)})
        }


Here I am using a apikey for basic security, you can add more security layer here based on your requirement.

{
  "ip": "8.8.8.8",
"apikey":"my-demo-key"
}


Curl Request:

curl -X POST -H "Content-Type: application/json" -d '{"ip":"8.8.8.8", "apikey":"my-demo-key"}' https://YOUR_API_ID.execute-api.YOUR_REGION.amazonaws.com/yourstage


Response:

{
    "status": "success",
    "country": "Vietnam",
    "countryCode": "VN",
    "region": "SG",
    "regionName": "Ho Chi Minh",
    "city": "Quận Bình Thạnh",
    "zip": "",
    "lat": 10.8085,
    "lon": 106.709,
    "timezone": "Asia/Ho_Chi_Minh",
    "isp": "Rainbow E-Commerce Company Limited",
    "org": "BIZMAC",
    "as": "AS131428 Rainbow E-Commerce Company Limited",
    "query": "45.117.76.25"
}
    


How to deploy:

Write your code

  • Create a Python, Node.js, or other supported language file (e.g. lambda_function.py).

Zip it

  • Package your code (and dependencies, if any) into a .zip file.

Create Lambda in AWS Console

  • Go to AWS Console → Lambda → Create function.
  • Choose “Author from scratch” or “Upload a .zip file”.

Upload your .zip

  • Under Code, upload your .zip file.

Set Handler & Runtime

  • Example: Handler = lambda_function.lambda_handler
  • Runtime = Python 3.x (or your language).

Configure permissions

  • Attach an IAM role that gives your Lambda the permissions it needs.

Test it

  • Use the Test button in the Lambda console. 



The detailed Demo video is here.


Hope you find it helpful!!!

Drop us email for our consultation!

Multi-threading in NestJS or nodejs