SkillSprout
Chapter 2 · Lesson 3
Advanced
5 min read

Lambda Aliases and Serverless Deployments

Overview

Serverless functions are deployed as immutable versions. Each time you publish, Lambda snapshots code and configuration as version 1, 2, 3 and so on, and an alias (such as live) is a named pointer to a version. Callers invoke the alias, so you can move the pointer without changing clients.

Think of a restaurant menu board: dishes (versions) are fixed recipes, and the board entry "Special of the day" (alias) points to one of them. You can show 90% of guests the old special and 10% the new one by splitting the pointer, and change it back in one second.

CodeDeploy and AWS SAM automate weighted alias shifting with hooks and alarms. The exam tests alias weights, pre- and post-traffic hooks, the SAM DeploymentPreference property and how API Gateway stages and Route 53 provide similar controls.

Key Concepts

  • Versions are immutable; $LATEST is mutable. Aliases point to one version or split weights across two versions.
  • CodeDeploy for Lambda shifts alias traffic as Canary (two steps), Linear (equal increments) or AllAtOnce.
  • Predefined configs include Canary10Percent5Minutes, Canary10Percent30Minutes, Linear10PercentEvery1Minute, Linear10PercentEvery10Minutes, AllAtOnce.
  • Hooks: BeforeAllowTraffic (pre-traffic) and AfterAllowTraffic (post-traffic) are Lambda functions that validate the new version.
  • In SAM, AutoPublishAlias and DeploymentPreference create the alias, CodeDeploy app and deployment group automatically.
  • Alarms attached to the deployment trigger automatic rollback to the old version.
  • Provisioned concurrency on an alias avoids cold starts for the new version; configure it on the alias, not $LATEST.
  • API Gateway supports canary release deployments on a stage, and stage variables can bind a stage to an alias.

Detailed Notes

Versions and aliases from the CLI

bash
aws lambda publish-version --function-name orders --description "v7"
aws lambda update-alias --function-name orders --name live \
  --function-version 7 \
  --routing-config AdditionalVersionWeights={"8"=0.1}

The alias live now sends 90% of invocations to version 7 and 10% to version 8. An alias weight split is only supported across two versions at a time, and neither can be $LATEST.

CodeDeploy appspec for Lambda

yaml
version: 0.0
Resources:
  - OrdersFunction:
      Type: AWS::Lambda::Function
      Properties:
        Name: orders
        Alias: live
        CurrentVersion: "7"
        TargetVersion: "8"
Hooks:
  - BeforeAllowTraffic: PreTrafficValidator
  - AfterAllowTraffic: PostTrafficValidator

The hook functions must call codedeploy:PutLifecycleEventHookExecutionStatus with Succeeded or Failed, using the DeploymentId and LifecycleEventHookExecutionId in the event. A failure aborts and rolls back.

SAM does it for you

yaml
Transform: AWS::Serverless-2016-10-31
Resources:
  OrdersFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.12
      Handler: app.handler
      CodeUri: src/
      AutoPublishAlias: live
      DeploymentPreference:
        Type: Canary10Percent5Minutes
        Alarms:
          - !Ref ErrorsAlarm
        Hooks:
          PreTraffic: !Ref PreTrafficFunction
          PostTraffic: !Ref PostTrafficFunction

SAM generates the alias, a CodeDeploy application and group, and the required role. CloudFormation then performs the traffic shift during stack updates.

Choosing a traffic configuration

TypeBehaviourUse
Canary10% then remainder after N minutesQuick signal then full shift
Linear+10% every N minutesSlow, smooth risk exposure
AllAtOnceImmediateDev environments

Alarms worth attaching

Function Errors or error-rate metric math, Throttles, Duration p99, and downstream business metrics such as failed checkouts. Alarms must evaluate quickly: use period of 1 minute and datapoints 1-2 for rollbacks that matter.

Other serverless release controls

LayerControl
API GatewayCanary setting on a stage splits percent traffic to a canary deployment; promote or delete
Route 53Weighted records pointing to different API Gateway custom domains or CloudFront distributions
CloudFrontContinuous deployment with staging distribution and header/weight policy
AppConfigFeature flags and gradual configuration rollout with alarm-based rollback, independent of code deploys

Lambda container and layer notes

Container-image functions deploy new image digests as versions, and layers are immutable versions; update functions to reference a new layer version explicitly. Use code signing to enforce trusted artifacts.

Rollback behaviour

If an alarm fires or a hook fails, CodeDeploy points the alias back to the original version with 100% weight. No code redeployment is necessary because the version still exists, which is why alias-based rollbacks are seconds long.

Hooks, SAM preferences and release control

Lambda deployments through CodeDeploy accept a PreTraffic hook (smoke-test the new version before any traffic) and a PostTraffic hook (validate after the shift). Hook functions must call codedeploy:PutLifecycleEventHookExecutionStatus with Succeeded or Failed, or the deployment times out and rolls back.

yaml
OrdersFunction:
  Type: AWS::Serverless::Function
  Properties:
    AutoPublishAlias: live
    DeploymentPreference:
      Type: Canary10Percent5Minutes
      Alarms: [!Ref ErrorsAlarm, !Ref LatencyAlarm]
      Hooks:
        PreTraffic: !Ref PreTrafficHook

Other preference types are Linear10PercentEvery1Minute, AllAtOnce and custom values. Related controls: versions are immutable (code and configuration), layers are versioned separately, code signing blocks unsigned packages, and AWS AppConfig separates configuration or feature flag rollout from code deployment with its own gradual strategy and alarm-based rollback. Container-image functions deploy by image digest and benefit from ECR scanning.

Real World Example

Scenario: a fintech's payment-authorisation function. The team uses SAM with Linear10PercentEvery10Minutes, alarms on error rate and p99 duration, and a PreTraffic hook that invokes the new version against a sandbox card processor.

One release has a serialization bug that appears only with certain card types. At 20% traffic the error alarm fires; CodeDeploy moves the alias back to the old version and the pipeline reports failure. Only a small share of authorisations saw errors, and no engineer had to run commands.

Architecture Diagram

Lambda alias traffic shifting with CodeDeploy
Architecture
AWS Cloud
API Gateway
Region

Alias: live

Version 790%
Version 810%
Alarmserrors / duration
CodeDeploy

Weighted alias routes a percentage of invocations to the new version.

  1. 1A new version is published and the deployment starts.
  2. 2The BeforeAllowTraffic hook validates the new version.
  3. 3The alias weights shift per the canary or linear configuration.
  4. 4Alarms watch errors and latency during the shift.
  5. 5On success the alias moves fully to the new version; on alarm it returns to the old one.

Best Practices

  • ✓Always invoke through an alias, never $LATEST, from event sources and clients.
  • ✓Use SAM DeploymentPreference with alarms and pre-traffic hooks for every production function.
  • ✓Choose alarm metrics tied to business impact, with short evaluation periods.
  • ✓Use AppConfig for feature flags to separate deploy from release.
  • ✓Keep versions for rollback and clean up old ones on a schedule.

Common Interview Questions

Exam Tips

DOP-C02 focus

  • Lambda traffic shifting = aliases + CodeDeploy (or SAM DeploymentPreference).
  • Lambda appspec hooks are only BeforeAllowTraffic and AfterAllowTraffic.
  • Alias weights work across two versions, not $LATEST.
  • "Separate release from deployment" = AppConfig feature flags.

Finished this lesson?

0 of 44 lessons done · 0% of the course