Ravindra BagaleCourses & study guides Track your progress

Guides

Your First AWS Lambda Function in Python (Step by Step in the Console)

To create your first AWS Lambda function in Python, open Lambda → Create function → Author from scratch, enter a name, choose the latest Python 3.x runtime and Create a new role with basic Lambda permissions, then click Create function. Edit lambda_handler(event, context) in lambda_function.py, click Deploy, create a test event and click Test. The response and the print() output appear on the screen, and the full logs go to CloudWatch Logs under /aws/lambda/<function-name>.

Come on, friends! So far we created servers, used SSH and installed Nginx. Today the opposite: no server, only code. In Lambda you write a function, and when something happens – a test click, a request to a URL, a file arriving in S3 – AWS runs that code. You pay only while the code runs. So the very first job is to create the function. Even if you don't know Python, don't worry – it is just ten lines.

Quick answer

In the Lambda console: Create function → Author from scratch → name hello-ravindra → runtime Python 3.14 (or the newest Python shown) → Create function. Replace the code, click Deploy, then Test:

import json

def lambda_handler(event, context):
    name = event.get("name", "mitra")
    print(f"Request for {name}")          # goes to CloudWatch Logs
    return {
        "statusCode": 200,
        "body": json.dumps({"message": f"Namaskar {name}! Hello from Lambda"})
    }

What do I need before creating a Lambda function?

  • An AWS account and an IAM user (not root) — see How to Create an IAM User with MFA.
  • Nothing to install. Everything happens in the browser. (Later you can deploy from your laptop with the AWS CLI.)
  • 15 minutes. Lambda has a monthly free allowance of requests and compute time; check the AWS Lambda pricing page for the current numbers.

What happens when a Lambda function is triggered?

What happens when a Lambda function is triggered A trigger such as a test event, a function URL call or an S3 upload sends an event to AWS Lambda. Lambda runs lambda_handler with the event, returns a 200 response with the message, and writes the logs to CloudWatch Logs. TriggerTest / URL / S3 200 "Namaskar Ravi!" eventresponse AWS Lambda def lambda_handler(event, context):return {200, body} ▶ running... CloudWatchLogs eventresponselogs

A trigger (a test event or a function URL call) sends an event to Lambda. AWS starts your Python code, runs lambda_handler(event, context), returns the response, and writes the logs to CloudWatch. You pay only while it runs.

Event = the input (a JSON document from a test, an HTTP call, an S3 upload, a schedule). Handler = the Python function Lambda calls with that event. Execution role = the IAM role that decides what your code may do in AWS; the basic role only allows writing logs.

How do I create my first Lambda function in Python?

Step 1 — Create the function

Choose your region (for example Asia Pacific (Mumbai)) → Lambda → Create function → Author from scratch:

Setting Value
Function name hello-ravindra
Runtime Python 3.14 (or the newest Python version in the list)
Architecture x86_64 (or arm64, which is usually cheaper; both work for this code)
Execution role Create a new role with basic Lambda permissions

Click Create function.

Step 2 — Write the code

The editor opens lambda_function.py with a sample. Replace it with the code from the quick answer above. Lambda calls the function named in Runtime settings → Handler: lambda_function.lambda_handler means file lambda_function.py, function lambda_handler.

Step 3 — Deploy

Click Deploy. Until you deploy, Lambda keeps running the old code. The editor shows that there are undeployed changes.

Ravindra Bagale's Tip

Many students change the code, press Test straight away and say "Sir, I'm getting the old output!" Because they never clicked Deploy. Pay attention: first Deploy, then Test – every time. And if you rename the handler, change it in Runtime settings too, otherwise you get the Runtime.HandlerNotFound error.

Step 4 — Create a test event and run it

Click Test → Create new test event → event name ravi-test → Event JSON:

{ "name": "Ravi" }

Save, then Test again. The result shows:

{
  "statusCode": 200,
  "body": "{\"message\": \"Namaskar Ravi! Hello from Lambda\"}"
}

Below it you see the Function Logs (with your print() line), the duration and the memory used. Your first serverless code just ran. ⚡

Step 5 — Read the logs in CloudWatch

Monitor tab → View CloudWatch logs → log group /aws/lambda/hello-ravindra → newest log stream. Every invocation writes a START, your print() output, an END and a REPORT line with the billed duration.

Step 6 — Optional: call the function from a browser

Configuration → Function URL → Create function URL → auth type NONE (for this short demo only) → Save. The console adds the permission the URL needs. When called through the URL, the query string arrives in event["queryStringParameters"], so update the first line of the handler and Deploy:

    name = (event.get("queryStringParameters") or {}).get("name") or event.get("name", "mitra")

Open https://<url-id>.lambda-url.ap-south-1.on.aws/?name=Ravi — the JSON message appears in the browser. A URL with auth NONE is public, so delete it after the demo (Function URL → Delete), or use AWS_IAM auth.

Ravindra Bagale's Tip

You kept the function URL on auth NONE and shared it in a WhatsApp group – now anyone can call it as many times as they like, and the bill comes to you. Delete the URL once the demo is over. And never write a password or access key in the code – use Environment variables or Secrets Manager. Don't forget this!

Step 7 — Settings worth knowing

Setting Default Where
Timeout 3 seconds Configuration → General configuration → Edit
Memory 128 MB (more memory also gives more CPU) Configuration → General configuration
Environment variables none Configuration → Environment variables
Permissions basic role (logs only) Configuration → Permissions → role name

To give the function access to, for example, one S3 bucket, add only that permission to its execution role — least privilege, the same idea as an IAM role on EC2. To remove everything: Actions → Delete function, then delete the log group in CloudWatch if you like.

How do I fix common Lambda errors?

Ghabru naka 😅 — these are the usual ones:

Symptom Likely cause Fix
Old output after a code change Not deployed Click Deploy, then Test
Runtime.​HandlerNotFound Handler name does not match file/function Runtime settings → Handler lambda_​function.​lambda_​handler
Task timed out after 3.​00 seconds Default timeout too short, or code waiting forever Raise the timeout; check network calls
KeyError: 'name' Using event["name"] and the event has no such key Use event.​get("name", "mitra")
AccessDeniedException calling S3, DynamoDB... Execution role lacks that permission Add only the needed permission to the role
Function URL returns Forbidden Auth type is AWS_IAM, or the URL permission is missing Use NONE for the demo, or sign the request; recreate the URL

Learn it properly

This guide is the short path. These free lessons explain the ideas around it:

Samajla ka? Got it? Create function, code, Deploy, a test event, and CloudWatch logs – your first serverless function is ready. No server, no patching, you pay only while the code runs. Now try triggering it from an S3 upload or a schedule.

Frequently asked questions

What is AWS Lambda?

A serverless compute service: you upload a function, AWS runs it when an event arrives (a test, an HTTP call, an S3 upload, a schedule) and you pay for the requests and the run time, with no server to manage.

What does lambda_handler(event, context) mean?

lambda_handler is the function Lambda calls. event holds the input data as a Python dict, and context holds information about the invocation, such as the request ID and the remaining time.

Why do my code changes not run when I click Test?

You did not deploy. In the console editor click Deploy first, then Test.

Where do I see print() output from Lambda?

In Amazon CloudWatch Logs, log group /aws/lambda/FUNCTION_NAME. The Test result in the console also shows the last lines of the log.

Why does my Lambda function time out?

The default timeout is 3 seconds. Raise it in Configuration → General configuration if the work really takes longer, and check that the code is not waiting for something that never answers.

Is a Lambda function URL with auth type NONE safe?

It is public: anyone with the URL can call it. Use it only for a short demo and delete it afterwards, or choose AWS_IAM auth or API Gateway with authentication for real apps.