Serverless: Lambda, functions as the unit of compute
AWS Lambda from first principles: the execution environment, synchronous, asynchronous and polling invocations, cold starts, concurrency, and production habits.
Part 1 of the Serverless series.
AWS Lambda runs your code in response to events, and you pay only for the time it runs. No servers, no idle cost, and it scales from zero to thousands of concurrent executions on its own.
The mental model
A Lambda function is your code plus configuration. When an event arrives, Lambda runs your handler with that event:
def handler(event, context):
name = event.get("name", "world")
return {"message": f"Hello, {name}!"}
Behind the scenes, Lambda creates an execution environment, a lightweight microVM, loads your code, runs any initialisation outside the handler, then calls the handler. After the invocation the environment is kept warm for a while and reused for later events. Creating a new environment is a cold start.
Key settings:
- Memory: 128 MB to 10,240 MB. CPU is allocated in proportion to memory, so more memory often makes a function faster and cheaper.
- Timeout: up to 15 minutes.
- Architecture:
arm64(Graviton) is cheaper per millisecond thanx86_64and usually just as fast. Make it your default. - Execution role: the IAM role the function’s code runs as.
Three ways to invoke a function
How a function is invoked decides how errors and retries behave, and it’s the most important thing to understand about Lambda:
| Model | Examples | Errors |
|---|---|---|
| Synchronous: the caller waits for the result | API Gateway, function URLs, AppSync, the SDK Invoke call |
Returned to the caller, who decides whether to retry |
| Asynchronous: Lambda queues the event and returns immediately | S3 notifications, EventBridge, SNS | Lambda retries twice, then sends the event to a failure destination or dead-letter queue |
| Event source mapping: Lambda polls a source and invokes with batches | SQS, Kinesis, DynamoDB Streams | Depends on the source; a failing batch can block a stream until it succeeds or expires |
Cold starts
Most invocations reuse a warm environment, but cold starts add latency, from tens of milliseconds for a small Node.js or Python function to seconds for a large Java one. To reduce them:
- Keep deployment packages small and import only what you need.
- Do expensive set-up (SDK clients, config) outside the handler, so warm invocations reuse it.
- Use SnapStart for Java, Python and .NET, which restores from a snapshot of an initialised environment.
- Use provisioned concurrency for latency-critical paths, accepting that you pay for it while idle.
For most event-driven work, cold starts simply don’t matter.
Concurrency
Each concurrent request needs its own environment. Your account has a regional concurrency limit (1,000 by default, and it can be raised), shared by every function. Two controls help:
- Reserved concurrency guarantees a function capacity and caps it. Useful to protect a database that can’t handle unlimited connections.
- Provisioned concurrency keeps environments initialised and ready.
Beyond a single request
Lambda keeps growing past “a function per request”. Two recent additions are worth knowing:
- Durable functions write multi-step workflows as ordinary code that can run for up to a year. Steps are checkpointed, and waits suspend the function without compute charges. They overlap with Step Functions, which compares the two.
- Managed Instances run Lambda functions on EC2 instances in your account that Lambda provisions, patches and scales for you. You pay EC2-based pricing plus a management fee instead of per-invocation pricing, which suits steady, high-volume workloads.
Hands-on: a function with SAM
# template.yaml
Transform: AWS::Serverless-2016-10-31
Resources:
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.13
Architectures: [arm64]
Handler: app.handler
CodeUri: src/
MemorySize: 256
Timeout: 10
FunctionUrlConfig:
AuthType: NONE
sam build && sam deploy --guided deploys it, and the function URL in the outputs is a working
HTTPS endpoint. (AuthType: NONE makes it public; fine for a demo, not for anything real.) Run
sam logs --tail in another terminal while you call it.
Production habits
- Use Powertools for AWS Lambda (Python, TypeScript, Java, .NET) for structured logging, metrics, tracing and idempotency.
- Set log retention on every function’s log group. The default is to keep logs forever.
- One function, one job. Small, focused functions with narrow IAM roles are easier to secure and reason about.
- Keep the handler thin. The handler should translate the event into a call to your domain logic, and the result back into a response. Keep business rules in their own layer, with no knowledge of Lambda, API Gateway or the AWS SDK, so you can unit test them without mocks of AWS and move them to another runtime if you ever need to. The Better Store, Part 3 shows this with the Onion Architecture, swapping a real payments client for a mock in tests.
- Don’t call Lambda from Lambda synchronously and wait. You pay for both while one waits; use a queue, EventBridge or Step Functions instead.
Next: API Gateway: the front door.
← Series overview