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.
चला मित्रांनो! आतापर्यंत आपण सर्व्हर बनवला, SSH केलं, Nginx इन्स्टॉल केला. आज उलटं: सर्व्हर नाही, फक्त कोड. Lambda मध्ये तुम्ही फंक्शन लिहिता, आणि काही घडलं – टेस्ट क्लिक, URL ला रिक्वेस्ट, S3 मध्ये फाइल आली – की AWS तो कोड चालवतो. कोड चालतो तेवढाच पैसा. तो सबसे पहला काम है फंक्शन बनवणं. Python येत नसेल तरी घाबरू नका – दहाच लाइन्स आहेत.
चलो दोस्तों! अब तक हमने सर्वर बनाया, SSH किया, Nginx इंस्टॉल किया. आज उल्टा: सर्वर नहीं, सिर्फ़ कोड. Lambda में आप फ़ंक्शन लिखते हो, और कुछ हुआ – टेस्ट क्लिक, URL पर रिक्वेस्ट, S3 में फ़ाइल आई – तो AWS वह कोड चलाता है. कोड जितना चलता है उतना ही पैसा. तो सबसे पहला काम है फ़ंक्शन बनाना. Python नहीं आता तब भी घबराओ मत – बस दस लाइनें हैं.
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?
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.
ट्रिगर (टेस्ट इव्हेंट किंवा function URL कॉल) Lambda ला इव्हेंट पाठवतो. AWS तुमचा Python कोड सुरू करतो, lambda_handler(event, context) चालवतो, रिस्पॉन्स परत देतो, आणि लॉग्स CloudWatch मध्ये लिहितो. कोड चालतो तेवढाच पैसा.
ट्रिगर (टेस्ट इवेंट या function URL कॉल) Lambda को इवेंट भेजता है. AWS आपका Python कोड शुरू करता है, lambda_handler(event, context) चलाता है, रिस्पॉन्स लौटाता है, और लॉग्स CloudWatch में लिखता है. कोड जितना चलता है उतना ही पैसा.
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.
Ravindra Bagale's Tip – मराठी
खूप स्टुडंट्स कोड बदलतात, लगेच Test दाबतात, आणि म्हणतात "सर, जुनाच आउटपुट येतोय!" कारण Deploy केलंच नाही. लक्ष द्या: आधी Deploy, मग Test – प्रत्येक वेळी. आणि handler चं नाव बदललं तर Runtime settings मध्ये पण बदला, नाहीतर Runtime.HandlerNotFound एरर येतो.
Ravindra Bagale's Tip – हिंदी
बहुत स्टूडेंट्स कोड बदलते हैं, तुरंत Test दबाते हैं, और कहते हैं "सर, पुराना ही आउटपुट आ रहा है!" क्योंकि Deploy किया ही नहीं. ध्यान दो: पहले Deploy, फिर Test – हर बार. और handler का नाम बदला तो Runtime settings में भी बदलो, वरना Runtime.HandlerNotFound एरर आता है.
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!
Ravindra Bagale's Tip – मराठी
Function URL auth NONE ठेवलं आणि URL WhatsApp ग्रुपवर शेअर केला – आता कोणी पण कितीही वेळा कॉल करू शकतो, आणि बिल तुम्हाला. डेमो झाला की URL डिलीट करा. आणि कोड मध्ये पासवर्ड किंवा access key कधीच लिहू नका – Environment variables किंवा Secrets Manager वापरा. बिलकुल विसरू नका!
Ravindra Bagale's Tip – हिंदी
Function URL auth NONE रखा और URL WhatsApp ग्रुप पर शेयर कर दिया – अब कोई भी कितनी भी बार कॉल कर सकता है, और बिल आपको. डेमो होते ही URL डिलीट करो. और कोड में पासवर्ड या access key कभी मत लिखो – Environment variables या Secrets Manager इस्तेमाल करो. बिल्कुल मत भूलना!
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.
समजलं का? Create function, कोड, Deploy, टेस्ट इव्हेंट, आणि CloudWatch लॉग्स – serverless चं पहिलं फंक्शन तयार. सर्व्हर नाही, पॅचिंग नाही, कोड चालतो तेवढाच खर्च. आता S3 अपलोड किंवा शेड्यूल ने ट्रिगर करून बघा.
समझ आया? Create function, कोड, Deploy, टेस्ट इवेंट, और CloudWatch लॉग्स – serverless का पहला फ़ंक्शन तैयार. सर्वर नहीं, पैचिंग नहीं, कोड जितना चले उतना ही ख़र्च. अब S3 अपलोड या शेड्यूल से ट्रिगर करके देखो.
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.