AWS Lambda Cold Starts Explained
Why cold starts happen, how to measure them, and proven techniques to minimize impact.
What You'll Learn
- Understand exactly what happens during a Lambda cold start
- Measure init duration in CloudWatch Logs
- Reduce cold start time with bundle optimization
- Use Provisioned Concurrency for latency-sensitive workloads
- Choose the right runtime for low-latency Lambda functions
Prerequisites
- Working knowledge of AWS Lambda
- Basic understanding of Node.js or Python runtimes
- CloudWatch Logs access
Step-by-Step Guide
Step 1
1. What Is a Cold Start?
When Lambda receives a request with no warm execution environment available, it must: download your deployment package, start the runtime (Node.js, Python, JVM, etc.), and run your initialization code outside the handler. This is the cold start. CloudWatch Logs report it as Init Duration. Subsequent requests reuse the warm environment and have no init cost.
Step 2
2. Measuring Cold Starts
In CloudWatch Logs Insights: filter @type = 'REPORT' | stats avg(@initDuration), max(@initDuration), count(@initDuration) by bin(1h). The presence of initDuration in a log line means that invocation had a cold start. Use AWS X-Ray to visualize cold vs. warm latency in traces. Benchmark before optimizing — not every function has a cold start problem.
Step 3
3. Reduce Bundle Size
Smaller ZIP = faster download + init. Use esbuild or webpack to tree-shake and bundle only what you need. Move dev dependencies out of production packages. For Node.js, avoid importing the entire AWS SDK v2 — use AWS SDK v3's modular client imports (@aws-sdk/client-s3) instead of the monolithic aws-sdk. Aim for < 5 MB unzipped.
Step 4
4. Move Work Outside the Handler
SDK clients, DB connections, and config loaded outside the handler persist across warm invocations: const db = new DynamoDBClient({}); exports.handler = async (event) => { /* use db */ }. This shifts initialization cost to the cold start and amortizes it across many requests. Keep global state immutable or safely reset between invocations.
Step 5
5. Provisioned Concurrency
Provisioned Concurrency pre-initializes a set number of environments so they're always warm. Configure it on a function alias or version. Cost: ~$0.015/GB-hour of provisioned concurrency plus normal invocation cost. Use Application Auto Scaling to schedule provisioned concurrency for predictable traffic peaks. For most workloads, a 200–500ms cold start is acceptable — quantify the business impact before paying for PC.
Further Reading & Resources
Ready to put this into practice?
Apply for a real Cloud AWS internship and ship production projects with mentorship.
