Deploying AWS Lambda Functions with Traffic Shifting
Lambda deployments do not copy files to servers. Instead, CodeDeploy shifts invocations from one function version to another by changing the weights of an alias. This lesson explains versions and aliases, the canary, linear and all-at-once configurations, validation hooks and how AWS SAM generates the CodeDeploy resources.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how Lambda versions and weighted aliases enable gradual traffic shifting.
- Distinguish between canary, linear and all-at-once deployment configurations for Lambda.
- Describe the BeforeAllowTraffic and AfterAllowTraffic hooks and how a hook reports its result.
- Explain how AWS SAM DeploymentPreference configures CodeDeploy, alarms and hooks.
Versions and Aliases
Publishing a Lambda function creates an immutable, numbered version containing the code and configuration. The unpublished, editable copy is $LATEST. An alias is a named pointer, such as live, to a version. Event sources and callers invoke the alias ARN, so changing which version the alias points to changes the code that runs without reconfiguring any caller.
An alias can point to two versions with a routing configuration that sends a percentage of invocations to the second version. CodeDeploy uses this weighted alias to shift traffic gradually: it starts with a small weight on the new version and increases it according to the deployment configuration until the alias points entirely to the new version.
Deployment Configurations
Lambda deployment configurations define how traffic moves from the current version to the target version. There are three families, described here and summarised in the table that follows.
Canary
A canary configuration shifts a fixed percentage of traffic, such as 10%, to the new version, waits for an interval, and then shifts the remaining 90% at once. The interval gives alarms time to detect problems while only a small share of users is affected. Examples are CodeDeployDefault.LambdaCanary10Percent5Minutes and LambdaCanary10Percent30Minutes.
Linear
A linear configuration shifts traffic in equal increments at equal intervals, such as 10% every minute, so the new version takes over gradually. Examples are LambdaLinear10PercentEvery1Minute and LambdaLinear10PercentEvery10Minutes, which takes about 100 minutes in total.
All at once
LambdaAllAtOnce shifts all traffic immediately. Hooks still run, which makes it suitable for test environments where validation is wanted but gradual exposure is not.
| Family | Example | Traffic pattern | Total shift time in the example |
|---|---|---|---|
| Canary | LambdaCanary10Percent5Minutes | 10%, then 100% after 5 minutes | 5 minutes |
| Linear | LambdaLinear10PercentEvery1Minute | +10% each minute | 10 minutes |
| All at once | LambdaAllAtOnce | 100% immediately | Immediate |
Custom configurations can define other percentages and intervals. When choosing, consider how long an alarm needs to detect a problem: a canary interval shorter than the alarm's evaluation period lets a faulty version reach 100% before the alarm fires.
The following diagram compares how each family shifts traffic to the new version.

Name the Lambda configuration
Validation Hooks
A Lambda AppSpec can name two hook functions. BeforeAllowTraffic runs after the new version is ready but before any traffic shifts, which suits tests that invoke the new version directly. AfterAllowTraffic runs after all traffic has shifted, which suits end-to-end checks of the live alias. A hook function receives a deployment ID and a lifecycle event hook execution ID and must report its result by calling PutLifecycleEventHookExecutionStatus with Succeeded or Failed within one hour; a failure or timeout fails the deployment and triggers rollback if configured.
version: 0.0
Resources:
- pricing:
Type: AWS::Lambda::Function
Properties:
Name: pricing
Alias: live
CurrentVersion: "14"
TargetVersion: "15"
Hooks:
- BeforeAllowTraffic: pricing-pre-traffic-check
- AfterAllowTraffic: pricing-post-traffic-checkValidate Lambda releases
AWS SAM Deployment Preferences
Writing the CodeDeploy application, deployment group and AppSpec by hand is unnecessary when a function is defined in AWS SAM. Setting AutoPublishAlias makes SAM publish a new version whenever the code changes and point the alias at it, and DeploymentPreference makes SAM create the CodeDeploy resources and perform the update through CodeDeploy.
PricingFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: pricing/
AutoPublishAlias: live
DeploymentPreference:
Type: Canary10Percent5Minutes
Alarms:
- !Ref PricingErrorsAlarm
- !Ref PricingLatencyAlarm
Hooks:
PreTraffic: !Ref PreTrafficCheckFunction
PostTraffic: !Ref PostTrafficCheckFunctionWhen CloudFormation updates the stack, the alias update is handed to CodeDeploy, which shifts traffic according to the type, monitors the alarms and runs the hooks. If an alarm fires or a hook fails, CodeDeploy moves the alias back to the previous version, and the stack update is rolled back. The hook functions themselves need permission to call codedeploy:PutLifecycleEventHookExecutionStatus and to invoke the version being tested; their names conventionally start with CodeDeployHook_ when the default SAM-created service role is used, because that role's policy allows invoking functions with that prefix.
Safe Releases for a Payment Function
A payment authorisation function handles 500 requests per second. A previous release introduced a bug that failed 2% of requests and took 20 minutes to notice. The team wants future faulty releases to affect as few requests as possible and to be reversed automatically.
-
Alarms
Create CloudWatch alarms on the alias's Errors metric and on a custom metric for declined authorisations, with a 1-minute period and two evaluation periods.
-
Configuration
Use Canary10Percent10Minutes so that 10% of traffic runs the new version for longer than the alarm needs to detect a regression.
-
Hooks
A PreTraffic hook invokes the new version with test authorisations and fails the deployment if any result is wrong, before any user traffic reaches it.
-
Rollback
Alarms in DeploymentPreference make CodeDeploy move the alias back to the previous version automatically.
A Lambda deployment hook function runs successfully but the deployment stays in progress and eventually fails. What is missing?
Show answer
The hook function must call PutLifecycleEventHookExecutionStatus with the deployment ID and lifecycle event hook execution ID to report Succeeded. Without the call, CodeDeploy waits until the hook times out.
Exam Tips
Remember for the exam
- CodeDeploy shifts Lambda traffic by changing a weighted alias between two versions; callers must invoke the alias.
- Canary shifts a percentage then the rest; linear shifts equal increments at intervals; all-at-once shifts immediately but still runs hooks.
- BeforeAllowTraffic and AfterAllowTraffic hook functions must call PutLifecycleEventHookExecutionStatus.
- In AWS SAM, AutoPublishAlias plus DeploymentPreference (Type, Alarms, Hooks) creates the CodeDeploy resources and enables alarm-based rollback.
- Make canary intervals longer than the time alarms need to detect a problem.