Layers, Versions, Aliases and Deployments
Overview
Once a function is in production you need safe ways to change it. Lambda provides versions (immutable snapshots), aliases (named pointers to versions) and layers (shared code and libraries) so you can reuse dependencies, roll out updates gradually and roll back instantly.
Think of a book publisher. Each version is a printed edition that never changes. An alias such as prod is the sign on the shelf that says "customers read edition 7". Moving the sign to edition 8 (or showing edition 8 to 10% of readers) changes behaviour without reprinting anything. A layer is a shared appendix that many books refer to.
These features appear in deployment questions: weighted aliases, CodeDeploy traffic shifting and what can and cannot be modified on a published version.
Key Concepts
$LATESTis the mutable working copy. Publishing creates an immutable numbered version (code and configuration).- An alias points to one version (or two with weights) and has a stable ARN used by event sources and callers.
- Weighted aliases split traffic between two versions, for example 90/10, for canary releases.
- CodeDeploy can automate alias traffic shifting: canary, linear or all-at-once with pre/post traffic hooks and alarm-based rollback.
- Layers hold libraries, runtimes or data; a function can use up to 5 layers and total unzipped size stays under 250 MB.
- Container images (up to 10 GB) are an alternative packaging format using ECR and base images.
- Environment variables belong to a version; aliases cannot override them, so use separate config or Parameter Store per stage.
- Function URLs give a function a dedicated HTTPS endpoint with IAM or no auth.
Detailed Notes
Versions and aliases
# Publish an immutable version from $LATEST
aws lambda publish-version --function-name OrdersFn --description "v8 fix tax rounding"
# Point the prod alias at version 8
aws lambda update-alias --function-name OrdersFn --name prod --function-version 8
# Send 10% of traffic to version 9, 90% to version 8
aws lambda update-alias --function-name OrdersFn --name prod \
--function-version 8 \
--routing-config AdditionalVersionWeights={"9"=0.1}
| Concept | Mutable? | ARN suffix |
|---|---|---|
| $LATEST | Yes | unqualified or :$LATEST |
| Version | No | :8 |
| Alias | Pointer can move | :prod |
Event sources and API Gateway integrations should reference the alias ARN, so promotion needs no integration change. Permissions (resource policies) are per version/alias too.
Layers
A layer is a zip extracted to /opt. For Python, put libraries in python/, for Node.js in nodejs/node_modules/.
mkdir -p layer/python && pip install requests -t layer/python
(cd layer && zip -r ../deps.zip .)
aws lambda publish-layer-version --layer-name http-deps \
--zip-file fileb://deps.zip --compatible-runtimes python3.12
Layers are versioned and immutable; functions refer to a specific layer version. They help share code and shrink deployment packages but still count toward the 250 MB limit.
Gradual deployments with SAM
Resources:
OrdersFn:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.12
Handler: app.handler
AutoPublishAlias: live
DeploymentPreference:
Type: Canary10Percent5Minutes
Alarms:
- !Ref ErrorsAlarm
Hooks:
PreTraffic: !Ref PreTrafficHookFn
SAM publishes a version, updates the live alias and instructs CodeDeploy to shift traffic. If the alarm fires, CodeDeploy rolls back automatically.
| Preference | Behaviour |
|---|---|
| Canary10Percent5Minutes | 10% first, remainder after 5 minutes |
| Linear10PercentEvery1Minute | +10% each minute |
| AllAtOnce | Immediate shift |
Container images
FROM public.ecr.aws/lambda/python:3.12
COPY app.py ${LAMBDA_TASK_ROOT}
CMD ["app.handler"]
Push to ECR, then create the function with --package-type Image. Images allow larger dependencies (ML libraries) and consistent local tooling.
Function URLs
aws lambda create-function-url-config --function-name F --auth-type AWS_IAM creates a built-in HTTPS endpoint. Use AWS_IAM or NONE (public). It is simpler than API Gateway but lacks usage plans, caching and request validation.
Rollback
Rollback is just pointing the alias to the previous version. Because versions are immutable, the old code and configuration are exactly as they were.
Layers in detail
A function can use up to 5 layers, and the unzipped function plus layers must stay within 250 MB. Layers extract to /opt; Python finds libraries in /opt/python, Node.js in /opt/nodejs/node_modules. Layers are versioned and immutable, and a function pins a specific layer version ARN, so publishing a new layer version never changes running functions until you update them.
mkdir -p layer/python && pip install requests -t layer/python
(cd layer && zip -r ../deps.zip python)
aws lambda publish-layer-version --layer-name shared-deps --zip-file fileb://deps.zip \
--compatible-runtimes python3.12 --compatible-architectures arm64
aws lambda update-function-configuration --function-name orders \
--layers arn:aws:lambda:eu-west-1:111122223333:layer:shared-deps:4
| Packaging choice | Limit | Use when |
|---|---|---|
| Zip upload (direct) | 50 MB zipped | Small functions |
| Zip via S3 | 250 MB unzipped total | Larger dependencies, SAM/CloudFormation packaging |
| Container image | 10 GB | ML libraries, custom OS packages, existing container tooling |
Version and alias routing
An alias can split traffic between two versions with a routing configuration, which is the mechanism behind canary shifts. Event sources and API stages should reference the alias ARN, never $LATEST.
aws lambda create-alias --function-name orders --name live --function-version 17
aws lambda update-alias --function-name orders --name live --function-version 17 \
--routing-config AdditionalVersionWeights={"18"=0.1}
Resource-based policies are attached to a version or alias too, so grant API Gateway or S3 permission to invoke the alias that you actually use. Setting Environment variables on $LATEST does not change published versions; publish again to capture new configuration.
Real World Example
A retailer ships a pricing function every day. They deploy with SAM using AutoPublishAlias: live and a Canary10Percent5Minutes preference with a CloudWatch alarm on 5xx errors. One release had a null-pointer bug; the alarm fired within two minutes at 10% traffic and CodeDeploy shifted everything back to the previous version automatically. Fewer than 1% of customers saw errors, with no manual intervention.
Architecture Diagram
Traffic shifts gradually from the old version to the new one behind a stable alias.
- 1A new version is published from the updated code.
- 2CodeDeploy shifts a small percentage of the alias to it.
- 3CloudWatch alarms watch error and latency metrics.
- 4The shift completes, or the alias reverts to the old version on alarm.
Best Practices
- ✓Reference aliases, never
$LATEST, from triggers and APIs. - ✓Use SAM DeploymentPreference with alarms and pre-traffic hooks.
- ✓Keep layers for stable shared libraries and pin layer versions.
- ✓Use container images for large dependencies or existing container workflows.
- ✓Externalise configuration to Parameter Store per environment.
Common Interview Questions
Exam Tips
DVA-C02 focus
- Gradual Lambda rollout with automatic rollback = CodeDeploy + alias + alarms.
- Provisioned concurrency requires a version or alias, not
$LATEST. - Max 5 layers; total 250 MB unzipped; images up to 10 GB.
- Versions are immutable; aliases are mutable pointers.
Finished this lesson?
0 of 46 lessons done · 0% of the course

