Lambda functions and the execution model
How AWS Lambda runs your code: events, handlers, execution environments, the init and invoke phases, memory, limits and permissions.
The AWS CLI, SDKs and credential profilesWorkload identity for EC2, Lambda and CI
Lambda functions and the execution model
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- You supply a handler. Lambda creates execution environments on demand and calls the handler once per event.
- An environment handles one request at a time on standard Lambda, then is frozen and may be reused or discarded.
- Code outside the handler runs during init, once per environment. Put clients and configuration there, never per-request data.
- You choose memory, and CPU scales with it. More memory can be cheaper if it shortens the run enough.
- The default timeout is 3 seconds and the maximum is 30 minutes, raised from 15 in November 2026.
- The execution role says what the function may do. A resource-based policy says who may invoke it.
Code without a server to keep
A function that resizes an image when one is uploaded might run fifty times on Monday and not at all on Tuesday. Keeping an instance alive for that means paying for idle time and patching an OS for a hundred lines of code. AWS Lambda runs your code only when something happens, scales by running more copies, and bills for requests and execution time.
The price of that is a contract: your code must start quickly, finish within a limit and keep nothing important in memory.
Events and handlers
You write a handler, a function that receives an event (JSON describing what happened) and a context object:
import time
print("init: loading config and creating clients")
calls = 0
def handler(event, context):
global calls
calls += 1
return {"calls_in_this_environment": calls}
Events come from three styles of caller. Synchronous: the caller waits, as with an API. Asynchronous: Lambda queues the event and returns at once, as with S3 notifications. Polled: Lambda itself reads from a queue or stream and hands you batches. Failure handling differs for each, which the next lesson covers.
Execution environments
Lambda runs your code in an isolated execution environment, a small virtual machine with your runtime. Its life has phases:
- Init: fetch code, start the runtime and extensions, run your top-level code.
- Invoke: call the handler. This repeats for later events.
- Shutdown: the environment is removed.
Between invocations the environment is frozen. Nothing runs, so background threads and unflushed logs can stall until the next request, if there is one.
Run that code as two separate processes, each calling the handler three times, and each prints init once and counts 1, 2, 3. That is the model: like food trucks, each sets up once, serves many customers in turn, and shares nothing with the truck next door. One environment serves one request at a time, so ten simultaneous requests need ten environments. (Lambda Managed Instances, a newer option that runs functions on EC2 capacity, differs.)
So initialize SDK clients, connections and parsed configuration outside the handler. Keep user data inside it, or one request’s data can leak into the next. /tmp also survives reuse; treat it as a cache.
Resources and limits
| Setting | Range |
|---|---|
| Memory | 128 MB to 10,240 MB |
| Timeout | 3 s default, up to 30 minutes |
/tmp storage |
512 MB to 10,240 MB |
| Synchronous payload | 6 MB request and response |
| Asynchronous payload | 1 MB |
| Package | 250 MB unzipped zip, or a 10 GB container image |
The 30-minute maximum arrived in November 2026; nearly every video and article says 15. A long limit on your side doesn’t help if the caller gives up first, as API Gateway does.
Memory is the performance dial: CPU and network capacity grow with it. You’re billed for memory multiplied by duration, so doubling memory is free if it halves the time. Set a timeout a little above the slowest healthy run so hangs fail fast. The init phase is billed too.
Two directions of permission
The execution role is the identity your code uses to call AWS, delivered as temporary credentials. See workload identity. Give each function its own narrow role. The function’s resource-based policy states who may invoke it, such as a particular bucket or API. Environment variables (4 KB total) are for configuration; keep secrets in Secrets Manager or Parameter Store.
A function can be attached to a VPC to reach private databases. It then follows that VPC’s routing, so it has no internet access without NAT or endpoints.
Versions and deployment
A published version is an immutable snapshot of code and configuration. An alias is a movable name pointing at a version, and can split traffic between two for a gradual release. Package with SAM or CDK instead of editing in the console. Runtimes are deprecated on a published schedule, so plan upgrades.
About the video
The embed is Be A Better Dev’s 13-minute “AWS Lambda Function Execution and Cold Start” from June 2022. Its chapters cover function execution, cold starts including code download, full and partial cold starts, trimming dependencies, provisioned concurrency and throttling. It predates the 30-minute timeout and current init billing, and its timing figures are dated. I judged it from chapters and description; its audio is flagged as original US English.
What was run: the handler was called by a small local Python script in two processes. That stands in for execution environments and is not Lambda; no Lambda code ran anywhere. Limits and lifecycle come from the Lambda developer guide, checked in November 2026.
Check yourself
A counter stored in a global variable gives different numbers on successive requests. Why?
Each execution environment has its own memory, and requests are spread across several. Environments also come and go. Keep shared state in a database or cache.
Your handler creates a new SDK client and database connection on every call. What would you change?
Create them outside the handler so warm invocations reuse them, and handle the case where a reused connection has gone stale.
A CPU-heavy function is slow at 128 MB. Nothing else is wrong. What do you try?
More memory, which brings proportionally more CPU. Measure duration and cost at several sizes.
A function is triggered by S3 but gets AccessDenied reading the object. Which policy do you look at?
The execution role, which grants the function's own calls. The resource-based policy only controls whether S3 may invoke it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.