Lambda
AWS Lambda runs your code in response to events and bills only for the time it runs. No servers to manage.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Write a Lambda handler
- Connect event sources
- Grant permissions with an execution role
- Configure memory, timeout and environment variables
Explanation#
A handler is a function that receives an event (an HTTP request, S3 upload, SQS message, schedule…) and a context object.
Event sources include API Gateway, function URLs, S3, SQS, SNS, DynamoDB Streams and EventBridge schedules.
The execution role defines what the function may access. Memory (128 MB–10 GB) also scales CPU; the maximum timeout is 15 minutes.
Pricing is per request plus GB-seconds of compute. Cold starts happen when a new execution environment is created.
Code example: An HTTP API handler#
export const handler = async (event) => {
const name = event.queryStringParameters?.name ?? 'world';
console.log(JSON.stringify({ msg: 'greeting', name })); // goes to CloudWatch Logs
return {
statusCode: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ message: `Hello, ${name}!` }),
};
};
// zip function.zip index.mjs
// aws lambda create-function --function-name hello --runtime nodejs22.x \
// --handler index.handler --zip-file fileb://function.zip \
// --role arn:aws:iam::123456789012:role/hello-lambda-role
// aws lambda create-function-url-config --function-name hello --auth-type NONE
GET https://<url-id>.lambda-url.ap-south-1.on.aws/?name=Asha
{"message":"Hello, Asha!"}This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The handler reads a query parameter from the event.
- console.log output is captured in CloudWatch Logs automatically.
- The return value is turned into an HTTP response.
- A function URL gives the function its own HTTPS endpoint.
Real-world use case#
Generate thumbnails when images land in S3, process orders from SQS, or run a nightly cleanup job on an EventBridge schedule.
Common mistakes#
Create SDK clients and database connections outside the handler so warm invocations reuse them.
The default timeout is 3 seconds. Set an appropriate timeout, and use queues for heavy work.
Best practices#
- One execution role per function, least privilege
- Initialise clients outside the handler
- Log structured JSON
- Use dead-letter queues or on-failure destinations for async events
Remember this#
Event in → handler → result out.
More memory = more CPU.
Max 15 minutes; default timeout 3 seconds.
Practice exercise#
Your task: Increase the hello function’s memory to 512 MB and timeout to 10 seconds.
Show solution
aws lambda update-function-configuration --function-name hello --memory-size 512 --timeout 10
Mini quiz#
What is the maximum Lambda timeout?
Want more? Practise all AWS MCQs · AWS interview questions
My notes#
Saved in this browser.