BrainyBeeBrainyBee
ExploreBlogStart Studying
HomeAWS Certified DevOps Engineer - Professional (DOP-C02)Lab: Implementing Serverless Canary Deployments with AWS CodeDeploy
Hands-On Lab820 words

Lab: Implementing Serverless Canary Deployments with AWS CodeDeploy

Implement deployment strategies for instance, container, and serverless environments

Lab: Implementing Serverless Canary Deployments with AWS CodeDeploy

This lab demonstrates how to implement an immutable deployment strategy (Canary) for serverless environments using AWS CodeDeploy. You will learn how to shift traffic between Lambda versions and use AppSpec files to manage lifecycle events.

[!WARNING] Remember to run the teardown commands at the end of this lab to avoid ongoing charges for provisioned resources.

Prerequisites

  • AWS CLI: Installed and configured with AdministratorAccess.
  • IAM Permissions: Ability to create IAM Roles, Lambda functions, and CodeDeploy applications.
  • Region: Use us-east-1 for consistency.
  • Tools: A terminal and a text editor.

Learning Objectives

  • Configure an AWS Lambda function with versioning and aliases.
  • Create a CodeDeploy Application and Deployment Group for a Lambda compute platform.
  • Execute a Canary deployment (Linear10PercentEvery1Minute).
  • Verify traffic shifting via the AWS CLI and Management Console.

Architecture Overview

Loading Diagram...
Figure 1 — Mermaid diagram

Step-by-Step Instructions

Step 1: Create the Execution Role and Initial Lambda

First, we need a service role for the Lambda and the initial code version.

bash
# 1. Create trust policy file echo '{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"Service": "lambda.amazonaws.com"},"Action": "sts:AssumeRole"}]}' > trust-policy.json # 2. Create IAM Role aws iam create-role --role-name brainybee-lambda-role --assume-role-policy-document file://trust-policy.json aws iam attach-role-policy --role-name brainybee-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole # 3. Create initial code echo 'exports.handler = async (event) => { return "Version 1.0"; };' > index.js zip function.zip index.js # 4. Create Function aws lambda create-function --function-name MyCanaryFunction --runtime nodejs18.x --handler index.handler --role $(aws iam get-role --role-name brainybee-lambda-role --query 'Role.Arn' --output text) --zip-file fileb://function.zip --publish
▶Console alternative

Navigate to Lambda > Create Function. Name it 'MyCanaryFunction'. Use Node.js 18.x. Create a new role with basic Lambda permissions. Once created, click 'Publish new version' under the Versions tab.

Step 2: Create the Alias and CodeDeploy Application

CodeDeploy requires an Alias to manage the traffic shifting between versions.

bash
# 1. Create Alias pointing to Version 1 aws lambda create-alias --function-name MyCanaryFunction --name live --function-version 1 # 2. Create CodeDeploy App aws deploy create-application --application-name MyServerlessApp --compute-platform Lambda # 3. Create Deployment Group # Note: Requires a CodeDeploy Service Role. For brevity, ensure your user has permissions. aws deploy create-deployment-group --application-name MyServerlessApp --deployment-group-name MyCanaryGroup --deployment-config-name CodeDeployDefault.LambdaCanary10Percent5Minutes --service-role-arn <YOUR_CODEDEPLOY_ROLE_ARN>

[!IMPORTANT] You must replace <YOUR_CODEDEPLOY_ROLE_ARN> with an IAM role that has the AWSCodeDeployRoleForLambda managed policy.

Step 3: Trigger a Canary Deployment

Now, update the function code and trigger CodeDeploy to shift traffic.

bash
# 1. Update code for Version 2 echo 'exports.handler = async (event) => { return "Version 2.0 - New Features!"; };' > index.js zip function.zip index.js aws lambda update-function-code --function-name MyCanaryFunction --zip-file fileb://function.zip --publish # 2. Create appspec.yaml cat <<EOF > appspec.yaml version: 0.0 Resources: - MyLambdaFunction: Type: AWS::Lambda::Function Properties: Name: "MyCanaryFunction" Alias: "live" CurrentVersion: "1" TargetVersion: "2" EOF # 3. Deploy aws deploy create-deployment --application-name MyServerlessApp --deployment-group-name MyCanaryGroup --revision '{"revisionType": "AppSpecContent", "appSpecContent": {"content": "'$(cat appspec.yaml | sed 's/"/\\"/g')'"}}'

Checkpoints

  1. Verify Deployment Status: Run aws deploy get-deployment --deployment-id <ID>. Look for Status: InProgress.
  2. Check Alias Weights: Run aws lambda get-alias --function-name MyCanaryFunction --name live. You should see RoutingConfig showing a percentage weight for Version 2.
  3. Test Output: Invoke the alias multiple times:
    bash
    aws lambda invoke --function-name MyCanaryFunction --qualifier live out.txt && cat out.txt
    You should see "Version 1.0" 90% of the time and "Version 2.0" 10% of the time initially.

Troubleshooting

ErrorCauseFix
Invalid RevisionSyntax error in appspec.yamlEnsure YAML is valid and indentation is correct.
AccessDeniedCodeDeploy lacks permissionAttach AWSCodeDeployRoleForLambda to the service role.
Alias already existsRe-running setupUse update-alias instead of create-alias.

Clean-Up / Teardown

bash
aws deploy delete-application --application-name MyServerlessApp aws lambda delete-function --function-name MyCanaryFunction aws iam detach-role-policy --role-name brainybee-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole aws iam delete-role --role-name brainybee-lambda-role rm trust-policy.json index.js function.zip appspec.yaml out.txt

Stretch Challenge

Modify the appspec.yaml to include a BeforeAllowTraffic hook. This hook should trigger a separate "Validation" Lambda function. If the validation function fails (returns an error), CodeDeploy should automatically roll back the deployment to Version 1.

Cost Estimate

  • AWS Lambda: Free Tier (1M requests/month).
  • AWS CodeDeploy: No additional charge for code deployments to AWS Lambda.
  • IAM/S3: Negligible costs.
  • Total Estimated Spend: $0.00 (within Free Tier).

Concept Review

FeatureInstance (EC2)Container (ECS)Serverless (Lambda)
AgentCodeDeploy Agent requiredNo agent (managed by ECS)No agent (managed by Lambda)
StrategyIn-place or Blue/GreenBlue/Green (Canary/Linear)Blue/Green (Canary/Linear)
Configurationappspec.yml in rootappspec.yaml in deploymentappspec.yaml in deployment
Traffic ControlELB/Target GroupsALB/Target GroupsLambda Aliases
All AWS Certified DevOps Engineer - Professional (DOP-C02) Study Resources

Related Notes

  • AWS Certified DevOps Pro: Deployment Strategies for Instance, Container, and Serverless Environments940 words
  • Mastering AWS Alerting and Automated Remediation1,050 words
  • Study Guide: Analyzing Failed Deployments in AWS940 words
  • Incident Analysis: Troubleshooting Failed Processes in AWS1,050 words
  • Mastering AWS Monitoring & Security Analytics: Logs, Metrics, and Findings1,050 words
  • AWS Log Analysis: Athena, CloudWatch Insights, and OpenSearch920 words
  • Analyzing Real-Time Log Streams with Amazon Kinesis Data Streams985 words
  • CloudWatch Anomaly Detection Alarms: Professional Study Guide820 words
  • AWS Application Storage Patterns: EBS, EFS, and S31,054 words
  • Lab: Automating Security Controls and Data Protection with AWS Secrets Manager and Config942 words
  • Master Study Guide: Automating Security Controls & Data Protection (AWS DOP-C02)1,184 words
  • Mastering AWS CloudFormation StackSets: Multi-Account & Multi-Region Orchestration895 words

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up.

Start Studying

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free
AWS Certified DevOps Engineer - Professional (DOP-C02) ResourcesExplore All HivesBlogHome

© 2026 BrainyBee. Free AI-powered exam prep.

Loading Diagram...
Flowchart, top to bottom. Client Request connects to Lambda Alias (live). B connects to Lambda Version 1 (Blue) ("90% Traffic"). B connects to Lambda Version 2 (Green) ("10% Traffic"). CodeDeploy connects to B ("Updates Alias Weights").