AWSDOP-C02
On this page
Beginner-friendly overview

Lambda

Lambda lets you run code without managing servers. You write a function, upload it to Lambda, and AWS handles everything else โ€” the servers, the operating system, the scaling, the availability. You pay only for the time your code is actually executing, rounded to the nearest millisecond.

The Core Idea

Traditional servers run all the time, waiting for work. Lambda flips this: your code sleeps until something triggers it, runs, and goes back to sleep. That "something" can be an HTTP request arriving at API Gateway, a file landing in S3, a message appearing in SQS, a scheduled timer, or dozens of other AWS events. Lambda integrates natively with most AWS services as a reaction engine.

Events in โ†’ function (cold or warm) โ†’ results out Sync callers (wait) API Gateway ยท ALB ยท SDK Async events (queued) S3 ยท SNS ยท EventBridge Polled sources SQS ยท Kinesis ยท DynamoDB Streams Lambda function Cold start (first hit) new env + runtime + init code Warm start (repeat hit) reuse env โ€” skip init, run handler Response returned to the sync caller Destinations (async) on-success / on-failure โ†’ SQS ยท SNS ยท EventBridge ยท Lambda Code sleeps until an event arrives; each concurrent request gets its own environment โ€” scaling is automatic.

Why It Matters

Lambda removes an entire category of operational work. No servers to patch, no capacity to plan, no idle time to pay for. If your function is called 10 times a day, you pay for 10 executions. If traffic spikes to 10,000 simultaneous requests, Lambda scales automatically โ€” no configuration needed.

This makes Lambda ideal for event-driven workloads: image processing when a photo is uploaded, sending a notification when an order is placed, transforming records as they stream through Kinesis, or running a scheduled report every night.

How a Function Works

You write a handler โ€” a function that receives an event object and a context object and returns a result. The event contains the data from whatever triggered the function (the HTTP request body, the S3 object key, the SQS message, etc.).

def handler(event, context):
    name = event.get("name", "World")
    return {"statusCode": 200, "body": f"Hello, {name}!"}

Lambda runs this function in an execution environment โ€” a small, isolated container managed by AWS. The first time your function is invoked, Lambda initializes a fresh environment (a "cold start"). If another request comes in shortly after, Lambda reuses the same environment (a "warm start"), skipping initialization. Code outside the handler runs only on cold starts, so you can cache database connections and configuration there.

Key Limits to Know

Lambda functions can run for a maximum of 15 minutes per invocation. They are stateless โ€” don't rely on local files persisting between invocations. The only writable local storage is /tmp (up to 10 GB), but it's temporary and may not be shared across invocations.

For work that takes longer than 15 minutes, or that needs persistent state or a full OS environment, use ECS Fargate or EC2 instead.

Lambda's ceilings โ€” remember 15 / 10 / 10: 15 minutes max runtime ยท 10 GB max memory ยท 10 GB max /tmp scratch disk. If a job outgrows any of the three, it has outgrown Lambda โ€” move it to ECS Fargate or EC2.