Lesson 1 of 9 · 22m
The Lambda Execution Model
What actually happens when a function runs — isolation, scaling, concurrency, statelessness, and why Lambdas behave differently from application servers.
Serverless starts with a useful lie: "just upload your function and it runs." Useful, because provisioning disappears. Dangerous, because the execution model still exists. A Lambda function is not a daemon; it is an isolated execution environment that the platform creates, reuses, freezes, and destroys as load changes.
The lifecycle you are actually buying
INIT load runtime, run module-level code, create clients
INVOKE run handler(event, context) once per invocation
SHUTDOWN destroy the environment, usually after idle periodsInitialization runs once per environment, not once per invocation, so reusable work belongs outside the handler:
import boto3
dynamodb = boto3.resource("dynamodb") # INIT: reused while warm
table = dynamodb.Table("orders")
def handler(event, context):
table.put_item(Item={"pk": event["orderId"], "status": "accepted"})The day-one mistake is creating database connections, loading large models, or parsing configuration inside the handler. Move reusable setup outward and lazy-load rare heavy paths.
Stateless means exactly that
You cannot assume two invocations hit the same machine, memory, filesystem, or process. AWS may reuse an environment, but reuse is an optimization, not a contract. Durable state belongs in DynamoDB, S3, ElastiCache, RDS, or an external service.
/tmp is useful ephemeral scratch space — currently configurable up to 10 GB — but it can leak between invocations in the same warm environment. Treat it as a cache, not a database, and clean up files you create.
Scaling is concurrency, not copies of your app
If 1,000 events arrive at once, Lambda may create many concurrent environments:
1,000 events -> 1,000 concurrent invocations -> 1,000 downstream connections or requestsYour function often scales faster than PostgreSQL, a legacy vendor API, or a rate-limited dependency. Serverless does not eliminate capacity planning; it moves the bottleneck downstream.
Reserved concurrency caps simultaneous executions and is a protection tool:
ImageResizeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: resize.handler
Runtime: python3.12
ReservedConcurrentExecutions: 25Provisioned concurrency is different: it keeps initialized environments warm. Reserve one for blast-radius control; use the other only for measured latency-sensitive traffic.
Field notes
context.get_remaining_time_in_millis()is more reliable than guessing how much work remains possible.- SDK clients created outside the handler can reuse TCP connections; creating them per invocation is expensive.
- Environment variables are process configuration, not runtime mutable state.
- Think of Lambda as an event processor rented by the millisecond, not a tiny VPS.
Was this lesson helpful?