Sub topic 3
Understanding and Reducing AWS Lambda Cold Starts
Assume you know Python, JSON, HTTP, and basic AWS IAM. A cold start occurs when Lambda creates a new execution environment before running your handler. Initialization includes starting the runtime, loading dependencies, and running code outside the handler. A warm invocation reuses an existing environment and usually starts faster.
Cold starts and performance
Keep initialization small. Import only required libraries, create reusable clients outside the handler, and avoid expensive work during startup. Smaller deployment packages also reduce initialization time. Measure duration rather than guessing, because runtime, memory size, dependencies, and network configuration all affect latency.
Concurrency controls
Concurrency is the number of simultaneous function executions. Lambda can scale rapidly, but account and function limits still apply.
- Reserved concurrency sets a function's maximum concurrency and guarantees that capacity is reserved for it.
- Provisioned concurrency keeps initialized environments ready, reducing cold-start latency.
- Without either setting, Lambda uses available unreserved capacity.
Reserved concurrency protects dependencies from overload. Provisioned concurrency costs more, so use it for latency-sensitive traffic.
| Setting | Main effect | Typical reason |
|---|---|---|
| Reserved concurrency | Limits and reserves capacity | Protect a database |
| Provisioned concurrency | Keeps environments initialized | Reduce interactive latency |
Packaging choices
A ZIP package is simple and works well for small functions. Layers share dependencies across functions but add version-management overhead. Container images support larger dependencies and familiar image tooling, but they increase build and deployment complexity. Choose the smallest package that meets the runtime requirements.
Repeatable deployment with SAM
AWS SAM lets you describe functions, permissions, triggers, and settings as code. A typical workflow is:
sam build
sam deploy --guidedStore the template and source in version control. Review changes before deployment, and set memory, timeout, environment variables, and concurrency explicitly.
Observability
Lambda automatically writes logs to CloudWatch Logs. Use structured log fields such as request IDs and operation names. CloudWatch metrics show invocations, errors, duration, and throttles. AWS X-Ray traces requests across Lambda and supported services, helping locate latency in downstream calls.
VPC connectivity
Attach a function to private subnets when it must reach VPC resources such as a private database. This can add startup latency and requires suitable security groups, route tables, and outbound access. Use VPC endpoints or a NAT gateway when private subnets need AWS service access.
Exercise
Which setting should you consider first for a latency-sensitive API that experiences cold-start delays?
Show answer
Measure the function, reduce initialization work, and then consider provisioned concurrency for the required traffic window.