AWS Certified Developer – Associate
Lambda SnapStart vs provisioned concurrency (DVA-C03)
According to the Lambda Developer Guide, SnapStart initializes a function when you publish a version, snapshots that environment and resumes new environments from the snapshot, with startup as low as sub-second. Provisioned concurrency keeps environments initialized and ready to respond in double-digit milliseconds, for an extra charge. SnapStart does not support provisioned concurrency.
How does SnapStart work?
With SnapStart, Lambda runs your initialization code when you publish a function version. It takes a Firecracker microVM snapshot of the memory and disk state of the initialized execution environment, encrypts it and caches it. New execution environments are then resumed from the snapshot instead of being initialized from scratch.
SnapStart works only on published versions and on aliases that point to versions, not on $LATEST. It supports the Java 11 and later, Python 3.12 and later and .NET 8 and later managed runtimes. Other managed runtimes and OS-only runtimes are not supported. It does not support provisioned concurrency, Amazon EFS or ephemeral storage above 512 MB.
For Java managed runtimes there is no additional cost. For the other runtimes you pay to cache each published version's snapshot and pay a restoration charge each time an environment is restored. AWS notes that SnapStart works best for functions invoked at scale.
What changes when code resumes from a snapshot?
One snapshot is the starting state for many execution environments. Unique IDs, secrets or random seeds created during initialization would be shared, so generate them after initialization. Every environment also returns the same hostname.
The state of network connections opened during initialization is not guaranteed after a restore. Connections made by an AWS SDK usually resume on their own. For others, such as a database connection, re-establish them in the handler or in an after-restore runtime hook. Avoid binding connections to fixed source ports.
Runtime hooks run code before the snapshot is taken or after a restore. In Python the Snapshot Restore for Python library provides @register_before_snapshot and @register_after_restore. A before-snapshot hook suits heavy work such as downloading files or loading models: a file it writes to /tmp is part of the snapshot. After-restore hooks must finish within 10 seconds or Lambda raises SnapStartTimeoutException.
When is provisioned concurrency the answer?
Provisioned concurrency is a number of pre-initialized execution environments allocated to a function version or alias. They respond immediately, and the feature is designed for double-digit millisecond response times. AWS says to use it when cold start requirements are too strict for SnapStart.
It cannot be used with $LATEST, and the event source must point at the alias or version that has it. It adds charges, while reserved concurrency does not. Reserved concurrency sets the maximum and minimum number of concurrent instances for a function, and provisioned concurrency on a function cannot exceed its reserved concurrency.
Example: a Java checkout function with a known peak of 400 concurrent requests that must all start in double-digit milliseconds needs 400 units of provisioned concurrency on the alias the API calls. SnapStart brings startup to as low as sub-second, which does not meet a double-digit millisecond requirement.
Where the exam catches people
- Believing SnapStart can be used on
$LATEST: it works only on published versions and aliases that point to them. - Believing SnapStart and provisioned concurrency can be combined: SnapStart does not support provisioned concurrency.
- Believing a database connection opened during initialization is still good after a restore: its state is not guaranteed. Reconnect in the handler or in an after-restore hook.
- Believing a large download belongs in an after-restore hook: after-restore hooks have 10 seconds. A before-snapshot hook does the work once and the result is in the snapshot.
- Believing reserved concurrency removes cold starts: it reserves and caps capacity. Provisioned concurrency is the setting that pre-initializes environments.