What serverless means
Serverless questions appear in almost every AWS-focused DevOps interview, usually as "when would you use Lambda instead of containers?" followed by cold starts, concurrency limits, retries and cost. This topic explains how Lambda actually runs your code, the three ways it gets invoked and what each does on failure, how scaling and concurrency work, how to package, deploy and secure functions, and how to operate them: observability, orchestration with Step Functions, idempotency and the cost traps.
Serverless, containers and VMs
Serverless means you don't manage servers or capacity: you hand the platform code (or a container image) and it runs it on demand, scales it, and bills only for what runs. On AWS the core serverless services are Lambda (functions), API Gateway, SQS, SNS, EventBridge, Step Functions, DynamoDB, S3 and Fargate (serverless containers).
| EC2 / VMs | Containers on EKS or ECS | Lambda | |
|---|---|---|---|
| You manage | OS, patching, scaling | Images, cluster or tasks, scaling rules | Code and configuration |
| Scales | Auto Scaling groups, minutes | HPA and Karpenter, seconds to minutes | Per request, in seconds, to zero |
| Billing | Per second while running | Nodes or tasks while running | Per request plus GB-seconds of run time |
| Max run time | Unlimited | Unlimited | 15 minutes per invocation |
| Good for | Legacy, special hardware | Long-running services, steady traffic | Event-driven glue, spiky or low traffic, scheduled jobs |
Lambda shines when traffic is spiky or low, work is event-driven (a file lands in S3, a message arrives, a schedule fires) and each piece of work is short. It is a poor fit for long-running jobs, steady high traffic (containers become cheaper), workloads needing GPUs or very low, predictable latency, and apps that hold many open connections.
How Lambda runs your code
Each function runs in execution environments: lightweight Firecracker micro-VMs that AWS creates, reuses and destroys for you. An environment handles one request at a time; if ten requests arrive together, Lambda runs ten environments.
The life cycle:
- Init: download the code, start the runtime (Node.js, Python, Java…), run your code outside the handler (imports, SDK clients, database connections). Limited to 10 seconds.
- Invoke: call the handler with the event. Repeats for every request that reuses this environment.
- Shutdown: after a period of inactivity, the environment is frozen and eventually destroyed.
A request that has to wait for a new environment to initialise is a cold start; one that reuses a warm environment is a warm start. Anything created outside the handler (clients, connection pools, cached config) is reused across warm invocations, which is why you create SDK clients at the top of the file, not inside the handler.
import boto3, os
table = boto3.resource("dynamodb").Table(os.environ["TABLE"]) # once per environment
def handler(event, context): # every request
table.put_item(Item={"pk": event["id"], "status": "received"})
return {"statusCode": 200}
Don't rely on warm state for correctness: environments can disappear at any time, and /tmp (512 MB by default, up to 10 GB) is per-environment scratch space, not storage.