Cold starts are the most discussed — and most misunderstood — performance topic in AWS Lambda. For many workloads they barely matter; for user-facing APIs with spiky traffic they can define the experience. This guide explains what actually happens during a cold start, how to measure it, and which fixes pay off for Node.js and TypeScript functions.
What a cold start is
When Lambda receives a request and has no idle execution environment for that function version, it creates one:
- Download and prepare the code (your deployment package or container image).
- Start the runtime (Node.js).
- Run your initialization code — everything at module scope: imports, SDK client creation, configuration loading.
- Run the handler for the request.
Steps 1–3 are the cold start. Later requests reuse the warm environment and only run step 4. Environments are reused for a while and then recycled; you can't control exactly when.
Two consequences are easy to miss:
- Concurrency creates cold starts. A burst of 100 simultaneous requests needs 100 environments; each new one starts cold, even if the function was "warm" a second ago.
- Initialization is billed. The init phase counts toward billed duration, so slow start-up costs money as well as latency.
Measure before you optimize
Every cold invocation writes an Init Duration value in the REPORT line of its CloudWatch log. A CloudWatch Logs Insights query gives you the distribution:
filter @type = "REPORT"
| stats count(*) as invocations,
count(@initDuration) as coldStarts,
avg(@initDuration) as avgInit,
pct(@initDuration, 99) as p99Init
by bin(1h)Look at three numbers: the share of invocations that are cold, the p99 init duration, and the p99 end-to-end latency users actually see. If cold starts are 0.5% of requests on a background queue consumer, stop here — there is nothing to fix.
Fix 1: ship less code
For Node.js the biggest lever is bundle size and the amount of code evaluated at start-up.
- Bundle with esbuild. AWS CDK's
NodejsFunctionbundles each handler with esbuild, tree-shaking unused code. A single bundled file loads much faster than anode_modulesdirectory with thousands of files. - Import only what you use. With AWS SDK for JavaScript v3, import individual clients (
@aws-sdk/client-dynamodb), never a monolithic SDK. - Watch heavy dependencies. ORMs, large validation or date libraries, and full-featured HTTP frameworks can add hundreds of milliseconds. Check your bundle with esbuild's metafile analysis and ask whether each large dependency earns its cost.
- Minify and enable source maps separately. Minification reduces parse time; source maps keep stack traces readable without bloating the runtime path if you upload them alongside but don't load them eagerly.
Fix 2: initialize smarter
Code at module scope runs once per environment, which is good for reuse — but only for work every request needs.
// Created once per environment and reused across invocations.
const dynamo = new DynamoDBClient({});
let pdfRenderer: PdfRenderer | undefined;
export const handler = async (event: Request) => {
if (event.format === "pdf") {
// Loaded only by the requests that need it.
pdfRenderer ??= new (await import("./pdf-renderer.js")).PdfRenderer();
return pdfRenderer.render(event);
}
return getItem(dynamo, event.id);
};- Create SDK clients at module scope so connections are reused.
- Lazy-load rarely used, heavy modules with dynamic
import(). - Fetch secrets and configuration once and cache them — Powertools Parameters provides caching with a TTL.
- Avoid network calls at start-up that every request doesn't need.
Fix 3: give it more memory
CPU scales with memory, and initialization is CPU-heavy: parsing and compiling JavaScript. Raising a function from 128 MB to 512 MB or 1 GB often cuts init duration substantially. Because duration also drops, the cost difference is frequently small. Measure with Lambda Power Tuning.
Fix 4: split functions by purpose
One "lambdalith" that handles every route bundles every dependency for every request. Splitting by route or bounded context keeps each bundle small. The opposite extreme — one function per tiny operation — multiplies the number of environments that need warming. A function per resource or per closely related group of routes is usually a good middle ground.
Fix 5: avoid unnecessary VPC attachment
Attaching a function to a VPC no longer adds the long cold starts it did years ago, since Lambda now creates network interfaces ahead of time. But VPC functions still need NAT or VPC endpoints to reach AWS services and the internet. Only attach functions that genuinely need private resources such as an RDS database.
Fix 6: provisioned concurrency, deliberately
Provisioned concurrency keeps a number of environments initialized and ready. It removes cold starts for that many concurrent requests — at a cost billed for every hour it is enabled.
Use it when:
- the path is user-facing and latency-sensitive,
- traffic is predictable enough to size it, and
- the cheaper fixes above aren't enough.
Scale it with Application Auto Scaling on a schedule or on utilization, so you don't pay for peak capacity at night. Note that Lambda SnapStart, which snapshots an initialized environment, is available for Java, Python, and .NET runtimes but not for Node.js.
What doesn't work well
- "Warmer" pings with scheduled events keep one environment warm, which doesn't help concurrent bursts and adds invocations. Provisioned concurrency does this properly.
- Switching languages for cold starts alone. Well-bundled Node.js functions start fast; the gains from rewriting are rarely worth the cost.
Summary
- Measure cold start share and p99 init duration first.
- Bundle with esbuild and trim heavy dependencies.
- Initialize clients once; lazy-load the rest.
- Give init-heavy functions more memory.
- Split oversized functions.
- Use provisioned concurrency only where it pays.
Our performance and reliability service applies this process to your functions with before-and-after measurements.