0%
AWS in Practice: Services, Limits, and Failure Modes
Account Foundations
Storage
Databases
Networking
Delivery and Visibility
Limits, Cost and Recovery
Lambda Execution Model and Cold Starts
Lambda is often described as running code without servers, which is true from the point of view of the bill and misleading from the point of view of behaviour. Your code runs in an execution environment: a small, isolated virtual machine with a runtime, your function's code and a slice of memory and CPU. Lambda creates these environments, reuses them, freezes them between requests and eventually destroys them. Almost every surprise people have with Lambda, from cold starts to stale connections to unexpected throttles, comes from not having a clear picture of that lifecycle. This lesson builds that picture first, then uses it to explain latency, scaling, invocation models and cost.
Firecracker micro VMs
Each execution environment runs inside a Firecracker micro VM, a lightweight virtual machine that AWS built for Lambda and Fargate. Firecracker boots in a fraction of a second and uses very little memory overhead, which lets Lambda pack thousands of environments onto a single host while keeping hardware virtualisation isolation between them. One environment belongs to one function version and handles one request at a time. Two concurrent requests to the same function always run in two different environments, never as two threads in one.
That single request per environment rule is the most important fact in this lesson. It means that concurrency in Lambda is the number of environments, that global variables are never shared between simultaneous requests, and that scaling means creating more environments.
The three phases
An environment's life has three phases. During Init, Lambda downloads the code, starts the runtime, and runs your initialisation code, which is everything outside the handler function: imports, SDK clients, configuration loading and database connections. During Invoke, Lambda calls your handler with the event, and the handler returns a response. Between invocations, the environment is frozen: its processes are paused, and nothing runs. During Shutdown, which happens after some period without traffic or when Lambda recycles environments, extensions receive a shutdown event and the environment is destroyed.
What survives between invocations
Because the same environment usually handles many requests in sequence, anything created during init or stored in global scope survives from one invocation to the next. SDK clients, connection pools, parsed configuration, compiled regular expressions and cached lookups all persist, which is why the standard advice is to create them outside the handler. A function that creates a new database connection inside the handler pays the connection cost on every request; one that creates it during init pays it once per environment.
The same persistence is a source of bugs. Data written to a global variable during one request is visible to the next request handled by that environment, which can leak one user's data into another user's response if code caches request specific values globally. Files written to /tmp persist the same way and count against the environment's ephemeral storage. And background work started during a request but not awaited does not run between invocations, because the environment is frozen; it resumes, possibly much later, when the next request arrives, which produces log lines and metrics with confusing timestamps or work that never completes.
Freezing and stale connections
A frozen environment holds its open network connections, but the other end does not know the environment is frozen. A database or a load balancer may close a connection that has been idle for several minutes, and the function discovers this only when it next tries to use the connection. Well written functions treat any pooled connection as possibly dead: they set short idle timeouts, catch connection errors and retry once with a fresh connection, or use a proxy such as RDS Proxy that manages connection lifetime on their behalf.
Environment reuse is not guaranteed
Lambda reuses environments when it can, but it makes no promise. Environments are recycled after a few hours even under steady traffic, rebalanced across hosts and zones, and destroyed during deployments when a new version is published. Code must work correctly when every request runs in a fresh environment, and should merely run faster when it does not. Treating environment reuse as an optimisation and never as a correctness assumption avoids a whole class of bugs that appear only under unusual traffic patterns.
Versions and aliases
Publishing a function creates an immutable version with its own number, and an alias is a named pointer to a version, such as prod pointing at version 42. Environments belong to a specific version, so publishing a new version and moving the alias means new requests go to new environments, which start cold. Aliases can split traffic between two versions by weight, which is the basis for canary deployments of Lambda functions through CodeDeploy, and provisioned concurrency, covered later, is configured on a version or alias rather than on the unqualified function.
Extensions
Extensions are processes that run alongside the function inside the execution environment, typically to collect telemetry, fetch secrets or forward logs. They take part in the lifecycle: they initialise during Init, adding to cold start time, they can keep running after the handler returns to flush data, which adds to billed duration, and they receive the shutdown event. A monitoring extension that adds two hundred milliseconds to every cold start and fifty to every invocation is not unusual, and is worth measuring before adopting.
Runtimes and architectures
Lambda provides managed runtimes for Node.js, Python, Java, .NET and Ruby, and the OS only runtimes for languages that compile to a native binary, such as Go and Rust. Container image packaging allows images up to ten gigabytes. Each function runs on either x86_64 or arm64, and the Graviton based arm64 architecture costs about twenty percent less per gigabyte second with similar or better performance for most code. As with EC2, the switch is easy for interpreted languages and depends on native dependencies having Arm builds.
Memory, ephemeral storage and the handler contract
The memory setting is the only resource knob a function has, and it sets more than memory. It decides the CPU share, the network bandwidth available to the environment, and the price of every millisecond. A function that runs out of memory is killed mid invocation with an error in the REPORT line showing maximum memory used equal to the configured size, and the request fails. Comparing Max Memory Used against Memory Size across many invocations shows whether a function is starved or over provisioned.
Ephemeral storage in /tmp is private to the environment, survives across its invocations, and disappears with it. It is useful as a cache for downloaded models or reference files that are expensive to fetch, and dangerous as a scratch area that is never cleaned, because a function that writes a new file per request eventually fills it and starts failing in long lived environments only. Clearing request specific files at the end of each invocation, or naming them so they are overwritten, avoids that slow failure.
The handler itself has a simple contract: it receives an event and a context object, and it returns a result or throws. The context exposes the remaining time before the timeout, the request ID and the function's identity. Using the remaining time to stop work cleanly, rather than being killed at the timeout, lets a function return a partial result or put unfinished work back on a queue. Logging the request ID with every line ties a request's logs together, which matters once one environment's log stream interleaves many requests over hours.
A mental model to keep
Picture a pool of identical small machines per function version, each with a warm cache of everything init created. Requests take an idle machine if one exists and wait for a new one to boot if not. Machines that sit idle are paused, and eventually thrown away. Almost every behaviour in the rest of this lesson, from cold starts to throttles to stale connections, is that picture seen from a different angle.
An execution environment handles exactly one request at a time, so concurrency is the number of environments. Anything created during init survives across invocations in that environment, which makes connection reuse fast and global request state dangerous. Code must be correct in a fresh environment and only faster in a reused one.