Speeding up EC2 and Lambda boot time
On this page
- Goal and situation
- Simple
- Operations engineer
- Architect
- Picture it as
- Mechanism: where the time goes
- Case A · Plain Lambda cold start
- Case B · EC2 scale-out with no warm pool
- Experiment: change one condition, predict the result
- Five solution cases: when they help, when they stop
- Case C · SnapStart shortens a Java cold start
- Case D · Warm pool shortens scale-out
- Case E · Requests exceed Provisioned Concurrency
- Case F · Warm pool runs dry mid-spike
- Case G · SnapStart reuses an old connection, then fixes it
- The speed-up toolbox
- When to use it, when not to
- Worth investing in speed
- Not yet, or choose something else
- Common myths
- Self check
- Question 1
- Question 2
- Question 3
- Handoff task
- Core terms
- Sources and unverified parts
Both Lambda and EC2 are slow at boot because they build an environment and run initialization before they can handle the first job. Every speed-up feature does one of three things: move that work off the request path, reuse a captured result, or remove work entirely.
Goal and situation
After this post you can explain why boot is slow, pick one feature for a specific situation, and predict when it stops helping. Prerequisite: a basic grasp of Lambda and EC2 Auto Scaling.
Simple
Whenever a guest arrives and nobody is ready, the guest waits while people get prepared. Speeding up means preparing before the guest arrives.
Operations engineer
A ticket-booking API runs on Lambda and a web fleet on EC2 Auto Scaling. At 9 a.m. traffic jumps five-fold; the first request and any new instance are the slow parts. Constraint: the cost of holding idle capacity must stay low.
Architect
Three levers, each with its own price. Prepare ahead (Provisioned Concurrency, warm pool) pays for idle capacity. Capture (SnapStart, a prebuilt AMI) pays with state constraints. Do less (optimize INIT) pays with engineering effort.
Picture it as
A restaurant at peak has to light the fire and prep before it cooks the first dish; a kitchen already prepped serves instantly. Lambda and EC2 at scale-out do the same: build an environment, run INIT or user data, then handle work. A snapshot or a prepared pool skips that part.
| Real life | AWS |
|---|---|
| Light the fire and prep | Build environment plus INIT (Lambda), boot plus user data (EC2) |
| Keep a chef standing at the station | Provisioned Concurrency: environments pre-run INIT; you pay even when the room is empty |
| Freeze a pot that is already prepped | SnapStart: capture an INIT-complete environment, reheat it on demand |
| Staff resting in the next room | Warm pool: initialized instances sit Stopped or Hibernated waiting for a shift |
Mechanism: where the time goes
| Service | Phase 1 | Phase 2 (usually longest) | Phase 3 |
|---|---|---|---|
| Lambda | Build environment (load code, start runtime) | INIT: code outside the handler | Handler |
| EC2 Auto Scaling | Launch plus OS boot | User data, install dependencies | App start plus health check |
Case A · Plain Lambda cold start
A Java ticket-booking API at 9 a.m. with no free environment. Illustrative numbers: 500 ms to build the environment, 1800 ms INIT, 200 ms handler.
Total ≈ 2.5 s
3 / 3
First request · no free environment
- A request arrives but no execution environment is free; Lambda loads the code and starts the runtime in 500 ms, on the request path.
- The INIT phase runs code outside the handler: load classes, create SDK clients, open connections. This case is 1800 ms, 72 percent of the total and the most expensive part.
- The handler runs for 200 ms. The user waits 2.5 seconds in total even though the business logic only needs 0.2 seconds.
Takeaway: the 1800 ms INIT is the main target because it repeats on every new environment and has nothing to do with business logic.
Case B · EC2 scale-out with no warm pool
A web fleet needs more instances at 9 a.m. Illustrative numbers: 40 s OS boot, 90 s user data, 50 s app plus health check.
InService ≈ 180 s
3 / 3
New instance · no warm pool
- An alarm triggers scale-out; the Auto Scaling group calls EC2 RunInstances from the AMI in the launch template, and the instance boots the OS in 40 seconds.
- User data installs packages, pulls artifacts, writes data to disk: 90 seconds. This is the part a prebuilt AMI or a warm pool can compress.
- The app starts and passes its health check; only then is the instance InService. About 180 seconds total while traffic keeps climbing.
Takeaway: the 90 s of user data does not depend on load, so doing it after traffic arrives is wasted. That is exactly what a warm pool or a prebuilt AMI targets.
Experiment: change one condition, predict the result
The original slider lab is not part of the blog, so predict first, then compare. The case diagrams above have a Play button that replays each phase in proportion to its time.
Set Provisioned Concurrency to 5 while 10 requests arrive at once: 5 requests still cold start because only 5 environments are pre-run, and the slowest request is still about 2.5 seconds, unchanged from having PC off. The value of PC is that the other 5 requests drop to 0.2 seconds. Turn SnapStart on too and those 5 fall to about 0.5 seconds.
Push the request count to 20 with PC = 10 and enable SnapStart: PC = 10 cannot lower the slowest request while SnapStart is off, because request 11 onward still goes through a full cold start.
The declarative model: requests and instances arrive at once with no environment reuse; 500 ms to build, 200 ms handler, 300 ms restore, and warm starts of 60 s (Stopped) or 40 s (Hibernated) are all illustrative assumptions. The model ignores cost, Lambda's own environment reuse, and the time to capture a snapshot.
Five solution cases: when they help, when they stop
| Case | Condition | Branch point | Result |
|---|---|---|---|
| C · SnapStart | Java Lambda, heavy INIT | Restore a snapshot instead of re-running INIT | 0.5 s instead of 2.5 s |
| D · Warm pool | Long EC2 boot, predictable load | Heavy work already done while parked | about 60 s instead of 180 s |
| E · PC exceeded | PC = 2, three requests | Third request has no ready environment | Request 3 still takes 2.5 s |
| F · Pool drained | Need 3 instances, pool has 2 | Third instance is not in the pool | Full cold start of 180 s |
| G · SnapStart stale state | Connection opened during INIT | Old connection is invalid after restore | Failure, then fixed by reconnecting |
Case C · SnapStart shortens a Java cold start
The same Java function as Case A, now with SnapStart on a published version. Supported on Java 11+, Python 3.12+ and .NET 8+ (check the current list).
Total ≈ 0.5 s
4 / 4
At publish (once)
First request after publish
- You publish a version: Lambda runs INIT right then, with no request waiting.
- Lambda captures a Firecracker microVM snapshot (memory plus disk) of the INIT-complete environment, encrypts and caches it.
- The first request arrives: Lambda restores the snapshot instead of re-running INIT, 300 ms.
- The handler runs for 200 ms. Total 0.5 seconds instead of 2.5 in Case A, an 80 percent drop in this illustrative example.
Takeaway: INIT still runs, but once at publish instead of on every cold start. Java is not billed extra for SnapStart; other runtimes pay for the cache and restore.
Case D · Warm pool shortens scale-out
The same web fleet as Case B, now with a warm pool of Stopped instances. The root device must be EBS.
InService ≈ 60 s
4 / 4
Background · instance in the warm pool
At scale-out · taken from the pool
- Before peak, the warm pool launches instances and runs all the user data while a lifecycle hook holds them back; the heavy work finishes in the background.
- The instances are stopped. In the Stopped state you pay only for EBS and Elastic IP, not for the instance.
- Scale-out happens: an instance starts again from the pool, 35 seconds, without re-running the heavy install.
- The app starts and passes health check: 25 seconds. InService after about 60 seconds instead of 180 in Case B.
Takeaway: you pay the EBS cost of a Stopped instance to buy back 120 seconds on every scale-out. The AWS docs warn: if the first boot is not meaningfully slow, you do not need a warm pool.
Case E · Requests exceed Provisioned Concurrency
PC = 2, three concurrent requests. Assumption: all three arrive at once.
Req 3: 2.5 s
3 / 3
Requests 1–2 · within Provisioned Concurrency
Request 3 · beyond PC = 2
- You set Provisioned Concurrency to 2: Lambda pre-runs INIT for 2 environments before peak. Requests 1 and 2 arrive together, each taking a ready environment and only running the handler, 200 ms.
- Request 3 arrives while both environments are busy. It exceeds PC, so Lambda builds a new environment the normal way: build plus INIT.
- Its handler finishes after 2.5 seconds. PC only covers the number of environments you configured; the overflow still pays a cold start.
Takeaway: size PC to the peak concurrency you want to protect, measured by metric, not to the average. The overflow still pays a cold start.
Case F · Warm pool runs dry mid-spike
Three instances needed, the warm pool has 2. The same cold start happens if an Availability Zone runs out of capacity.
Instance 3 ≈ 180 s
2 / 2
Instances 1–2 · from the warm pool (pool = 2)
Instance 3 · warm pool drained
- The spike needs 3 new instances, but the warm pool holds only 2. Instances 1 and 2 come from the pool: warm start, InService after about 60 seconds.
- The pool is empty, so the instance launches straight into the Auto Scaling group: a full cold start, and instance 3 is up after 180 seconds.
Takeaway: the default pool size is max capacity minus desired capacity; if the peak is larger, set MaxGroupPreparedCapacity or accept the remainder coming up slowly.
Case G · SnapStart reuses an old connection, then fixes it
The function opens a DB connection during INIT. Per the AWS docs, a connection open at INIT is not guaranteed to survive a resume from snapshot.
Total ≈ 0.7 s
5 / 5
Request after restore · unfixed code
Request after restore · fixed code
- The code opens the DB connection during INIT, so that connection is in the snapshot. A request arrives, Lambda restores the snapshot in 300 ms.
- The handler uses the connection from the snapshot. Its state is not guaranteed after restore, so the call fails or hangs (600 ms in this example).
- The fix: same 300 ms restore, but reopen the connection right after resume, in the handler or an after-restore hook.
- Add 200 ms to open a fresh connection immediately after restore, before any business work.
- The handler uses the fresh connection and finishes, 0.7 seconds total. SnapStart is still far faster than 2.5 seconds, but you must handle the state the snapshot does not preserve.
Takeaway: reopen connections in the handler or an after-restore hook; do not use the hostname as a unique identifier; avoid pinning a fixed source port. Symptom: connection errors only on the first request after restore, not in a warm environment.
The speed-up toolbox
| Lambda | Cuts which phase | Trade-off | Use when |
|---|---|---|---|
| SnapStart | INIT into a restore | Only a few runtimes; must handle post-restore state | Java, Python, .NET; heavy INIT, bursty load |
| Provisioned Concurrency | Both build and INIT | You pay while idle; covers only what you configure | You need steady latency on predictable load |
| Reduce INIT work | INIT | Code effort, no infrastructure cost | Always worth doing first, every runtime |
| Understand INIT billing | Does not cut time | AWS standardized INIT billing for managed runtimes | When frequent cold starts inflate cost |
| EC2 | Cuts which phase | Trade-off | Use when |
|---|---|---|---|
| Warm pool (Stopped) | Boot plus user data | EBS and EIP cost; needs EBS root; no weighted mixed groups | Long boot, sudden scale-out |
| Warm pool (Hibernated) | Boot, user data and app init | Must meet hibernation conditions; RAM saved to EBS | App in-memory init takes very long |
| Instance reuse policy | The next scale-out | You must manage pool size | Load rises and falls many times a day |
| Prebuilt AMI (golden AMI) | User data, install dependencies | Rebuild the AMI when versions change | Fixed install steps, no pool needed |
| T family: CPU credit (different kind) | Does not cut boot | CPU burst while running; throttled when credits run out | Low load with short spikes; not a boot solution |
When to use it, when not to
Worth investing in speed
- The first request touches a latency SLA (a user-facing API).
- INIT or user data is longer than the actual work.
- Load spikes suddenly but is predictable.
- You measured and cold starts dominate p99.
Not yet, or choose something else
- Background jobs that tolerate a few slow seconds, skip it.
- Steady load with few cold starts: optimizing INIT is enough.
- Uncertain load you will not pay to wait on: use SnapStart or accept it.
- Already-fast boot: a warm pool only adds cost.
| Situation | Choose | Why |
|---|---|---|
| Java API, high p99 after deploy, tight budget | SnapStart, then optimize INIT | No SnapStart fee for Java; cuts the repeated INIT |
| API needs fixed latency during work hours | Provisioned Concurrency on a schedule | Covers exactly the peak concurrency |
| EC2 web boot 3 minutes, daily 9 a.m. spike | Warm pool Stopped (or golden AMI) | Do the heavy work ahead, pay only EBS |
| Instance holds a lot of data in RAM after init | Warm pool Hibernated | RAM is saved and reloaded |
Common myths
| Myth | Reality |
|---|---|
| SnapStart works for every runtime | Java, Python, .NET; check the current list |
| Provisioned Concurrency ends all cold starts | Only within the number of environments you set |
| SnapStart preserves all state | Connections, hostnames and random values can go stale |
| Keep the warm pool Running for speed | The docs advise against it: billed like a running instance |
| A warm pool guarantees every new instance is warm | When it drains, launches are cold |
| T-family CPU burst boots faster | Burst is CPU while running; it does not shorten boot |
Self check
Question 1
A Lambda function on Java 17 with Spring takes 3 seconds on the first request after each deploy. Load is erratic; the team wants lower latency but will not pay to hold capacity. What do you pick first?
Answer: enable SnapStart on a published version. Java is supported, INIT runs once at publish, and Java is not billed extra for SnapStart. Provisioned Concurrency works but breaks the idle-cost constraint; raising the timeout only masks deadline errors; moving to EC2 changes the problem instead of solving it.
Question 2
A function sets Provisioned Concurrency to 50. Peak has 120 concurrent requests. What is true of the 70 that exceed it?
Answer: they run on new environments and pay a cold start as if PC were off. Only 50 environments are ready; the rest must build one, like request 3 in Case E. PC does not grow with load, and the overflow is not rejected.
Question 3
An Auto Scaling group has a warm pool of 4 Stopped instances. A spike needs 10 more. What happens?
Answer: 4 warm-start and 6 cold-start. With 4 in the pool, only 4 come from it; the other 6 launch straight into the group, like Case F. The pool does not grow itself, and Auto Scaling does not skip requests.
Handoff task
An ECS service on EC2 takes 4 minutes to load a 2 GB model; traffic triples at 9 a.m. daily. What do you choose and why?
Grading criteria: recognize that the heavy part (loading the model) does not depend on load and can be done ahead; pick a warm pool, a golden AMI with the model baked in, or scheduled scaling before 9 a.m., and state the cost trade-off; state the limits that a drained pool cold-starts and that ECS needs its own config so instances do not join the cluster too early.
Core terms
| Term | Meaning |
|---|---|
| Cold start | A request waits while a new environment is created and initialized before the handler runs |
| INIT | The Lambda phase that runs code outside the handler: load libraries, create clients, open connections |
| SnapStart | Capture a Firecracker microVM snapshot after INIT at publish; new environments restore from it |
| Provisioned Concurrency | The number of Lambda environments pre-run and kept warm; you pay even while idle |
| Warm pool | A group of initialized instances held beside the Auto Scaling group in Stopped, Running or Hibernated state |
| Lifecycle hook | A pause point that lets you run custom work before an instance is stopped or put into service |
| InService | The state where an instance belongs to the group and receives traffic |
Sources and unverified parts
- Lambda SnapStart and best practices: the snapshot mechanism, supported runtimes, connection and hostname caveats.
- EC2 Auto Scaling warm pools: states, pool size, limits, drained pool.
- AWS Compute Blog: standardized INIT billing.
Slow boot is the cost of doing everything only after the load arrives. Each speed-up moves part of that work earlier, or removes it. But preparing ahead only covers what you actually prepared. Measure first, then pick the cheapest lever that covers the peak.
Related posts
AWS IAM policies: follow the request, find the Deny
Animated AWS IAM walkthroughs explain policy types, permissions boundaries, KMS, cross-account access and why one matching Deny overrides every Allow.
AWS acceleration: pick the right service for the slow leg
Six AWS acceleration services — CloudFront, Global Accelerator, S3TA, VPN, DAX, ENA/EFA — chosen by the slow leg, caching and protocol.
Attach AWS WAF in the right place and close the bypass
Where AWS WAF can attach, how ALB differs from NLB, body inspection limits and locking the origin, with six scenarios and config examples.
