AWS · Compute & Storage

Lambda

AWS Lambda runs your code in response to events and bills only for the time it runs. No servers to manage.

Beginner 5 min Lesson 8 of 30
0/30 in AWS
On this page · 5 min · Beginner

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#

JavaScriptindex.mjs Illustrative
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
Expected output
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#

  1. The handler reads a query parameter from the event.
  2. console.log output is captured in CloudWatch Logs automatically.
  3. The return value is turned into an HTTP response.
  4. 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#

Creating clients inside the handler

Create SDK clients and database connections outside the handler so warm invocations reuse them.

Default timeout for slow work

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
Shell Illustrative
aws lambda update-function-configuration --function-name hello --memory-size 512 --timeout 10

Mini quiz#

Lambda quizMultiple choice Question 1 of 2

What is the maximum Lambda timeout?

Score so far: 0/0

Want more? Practise all AWS MCQs · AWS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.