Lesson 5 of 12 AWS Lambda

Releasing Function Changes with Versions and Aliases

This lesson explains how to release and roll back function changes safely. Versions freeze a function's code and configuration, and aliases give clients a stable name that can be moved between versions or split traffic across two of them.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain what a published version captures and why it cannot be changed.
  • Describe how an alias points clients at a version and shifts a percentage of traffic to a new version.
  • Distinguish qualified and unqualified ARNs and identify which one a trigger or permission should use.

Versions

Lambda versions capture the code and configuration of a function at a point in time. While developing, you edit $LATEST, which is the only version that can be changed. Publishing creates a numbered version, such as version 6, which is an immutable snapshot of the code and its configuration, including the runtime, memory, timeout and environment variables, with its own ARN. Version numbers only increase and are never reused.

Immutability is what makes versions useful for releases: a caller that invokes version 6 always runs exactly the code and settings that were tested as version 6. It also means that changing anything, even a single environment variable, requires publishing a new version. Callers are rarely pointed at version numbers directly, because every release would then require changing every caller; aliases, described next, solve that.

Aliases and traffic shifting

An alias, such as prod, is a named pointer to a version. Clients and event sources call the alias, so you can change the version behind it without reconfiguring them. An alias can also split traffic between two versions by weight, which is how gradual deployments work.

A gradual deployment limits the impact of a faulty release. In a canary deployment, a small share of traffic, such as 10%, goes to the new version for a fixed period; if no alarm fires during that period, all remaining traffic moves at once. In a linear deployment, traffic moves in equal steps at a fixed interval, such as 10% every minute, so the new version takes full load only after ten steps. An all-at-once deployment moves all traffic immediately, which is fastest but exposes every user to a fault. Canary deployments detect problems quickly with little exposure, whereas linear deployments suit changes whose problems appear only under increasing load.

The following table summarises the three deployment types and their CodeDeploy names.

Deployment typeExample configurationBehaviour
CanaryCanary10Percent5Minutes10% of traffic for 5 minutes, then the rest
LinearLinear10PercentEvery1MinuteAdds 10% of traffic every minute
All at onceAllAtOnceShifts all traffic immediately

CodeDeploy, or the AWS SAM DeploymentPreference setting, automates these deployments and rolls back automatically when a CloudWatch alarm fires.

Example. A manual canary: send 10% of the live alias's traffic to version 6 while version 5 keeps the rest, then complete the release.

aws lambda publish-version --function-name orders-api          # returns "Version": "6"

aws lambda update-alias --function-name orders-api --name live \
  --function-version 5 \
  --routing-config 'AdditionalVersionWeights={"6"=0.1}'

# When version 6 looks healthy, shift all traffic and clear the weights
aws lambda update-alias --function-name orders-api --name live \
  --function-version 6 --routing-config 'AdditionalVersionWeights={}'

Qualified and unqualified ARNs

A function can be invoked through three kinds of ARN, and permissions and event sources apply to the exact ARN they name.

ARNExampleInvokes
Unqualified...:function:orders-api$LATEST
Version-qualified...:function:orders-api:6Version 6, which never changes
Alias-qualified...:function:orders-api:liveWhichever version or versions the alias points to

A resource-based policy statement granted on the unqualified ARN does not cover an alias, so a trigger that invokes orders-api:live needs a statement for that alias. This is the usual cause of an event source or API stage that fails only after being pointed at an alias.

Worked example

Releasing version 8 safely

API Gateway calls the prod alias of a function, which points to version 7. The developer publishes version 8 and wants to test it on a little real traffic:

StepState of the prod alias
Publish version 8100% version 7
Add a routing weight90% version 7, 10% version 8
Errors stay normal for an hourMove the alias to version 8 with no extra weight
A problem appears next dayPoint the alias back to version 7: an instant rollback

Nothing in API Gateway changed at any point, because it always calls the alias. Had it called $LATEST, every code upload would have gone straight to production. In practice SAM's DeploymentPreference automates exactly these steps with CodeDeploy and alarms.

Check yourself

Why should a trigger or API point to an alias rather than to $LATEST or a numbered version?

Show answer

An alias is a stable name that can be moved to a new version, or can split traffic between two versions, without changing the trigger. $LATEST changes with every code update, and a numbered version would force every caller to be updated for each release.

Exam Tips

Remember for the exam

  • Point event sources and API Gateway at an alias, not a version number, so traffic can shift without reconfiguring them.
  • A gradual rollout with automatic rollback calls for CodeDeploy or SAM DeploymentPreference.
  • Provisioned concurrency cannot be set on $LATEST; use a version or alias.
  • The fastest Lambda rollback is to point the alias back at the previous version.
  • Invoke permissions apply to the exact ARN named, so each alias used by a trigger needs its own permission.