Implementing Canary Deployment with AWS Lambda and AWS API Gateway.

Okey Ebere
5 min readAug 17, 2024

--

Graphical Illustration of Canary Deployment

Have you ever felt that nervous tension in your stomach when pushing a new update to production? You’re not alone. Traditional deployments can be risky, potentially impacting all your users if something goes wrong. This is where Canary deployments come into play! They enable you to roll out new versions to a small subset of users initially, minimizing risk and ensuring a smooth rollout.

Imagine testing a new feature in a small focus group before a full launch. Canary deployments work similarly. You release the new version of your application change to a limited group of users. If the new version performs well, you can gradually roll it out to more users. If there are issues, the impact is limited, and you can easily roll back.

In this article, we’ll explore how to implement safe and controlled canary deployments using AWS API Gateway and AWS Lambda.

How Does a Canary Deployment Work with AWS Lambda?

canary deployment

Canary deployments in AWS Lambda enable the testing of new Lambda function versions on a limited subset of requests. This facilitates early detection of potential issues by allowing a comparison of the new version’s results with those of the currently deployed version.

AWS Lambda supports the creation of multiple versions for a single function. Each version possesses its own codebase and associated dependencies, along with configurable settings such as memory allocation, timeout duration, and environment variables. To reference a specific version, Lambda Aliases are used. These Aliases function as human-readable identifiers that point to specific Lambda function versions.

Weighted alias traffic shifting in AWS Lambda streamlines the process of implementing canary deployments. This feature enables you to gradually shift traffic from the current function version to the new one by specifying a percentage of requests to be routed to the new version. Over time, the traffic distribution is automatically adjusted, allowing for a smooth transition.

Benefits of Canary Deployments

  • Reduced Risk: By exposing only a limited portion of your user base to the new version initially, you minimize the potential impact of unforeseen issues.
  • Early Detection: Canary deployments enable you to identify problems quickly, allowing for prompt fixes before they affect a wider audience.
  • Improved Confidence: Successful canary deployments boost your confidence in the stability of the new version, paving the way for a safe full rollout.
  • Minimized Downtime: Issues are caught and addressed during the canary phase, preventing widespread downtime during the final deployment.

Understanding AWS API Gateway and AWS Lambda

  • API Gateway: Acts as a single entry point for your serverless APIs, accepting requests from clients and routing them to appropriate backend resources, including Lambda functions.
  • Lambda: Serverless compute service that allows you to run code without managing servers. Lambda functions are perfect for handling backend logic in a scalable and cost-effective manner.

PREREQUISITES

  • An existing API Gateway REST API with a method integrated with your Lambda function. Refer to my GitHub link for the Terraform setup script.
  • Basic understanding of AWS resources like API Gateway, Lambda functions, and IAM roles.
  • AWS Account with appropriate permissions to create a version from the existing Lambda function and update API Gateway configurations.

Steps:

  1. Deploy the New Lambda Version: Create a new version of your Lambda function with the desired updates or changes. Ensure this version is functional and ready for testing.
  • Go to the AWS Lambda console and navigate to your specific lambda function.
  • Click on the “Code” tab. Here, you will make the necessary updates or changes to your Lambda function’s code.
  • Once you have made the desired modifications and deployed.
# stable version of the code 

import json

def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from canary deployment!')
}

# New Update
import json

def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from canary deployment, New Update!')
}
  • Click on the “Versions” tab. This will display a list of all existing versions of your Lambda function that have been published.
  • The unpublished version ($LATEST) is the version currently deployed and invoked by your API Gateway.
  • To create a new version, click the “Publish new version” button.
  • This will create a new version with a unique description (e.g., stable, v1, etc.).

2. Define the Alias:

  • In the “Aliases” tab, Click and select “Create alias.”
  • In the “Create alias” dialog:
  • Enter a descriptive name for the alias.
  • In the “Version” dropdown, select the newly created version number.
  • Click on ‘Weighted alias’’and choose the stable version
  • Then choose the Weight percentage(%).
  • Click “Create.”

By creating a weighted alias , You can shift traffic between two versions, based on weights (%) that you assign.

3. Create a Canary Deployment Stage:

  • In the AWS API Gateway console, navigate to your API.
  • Edit the API Gateway resource.
  • Navigate to Integration Request and click Edit.
  • Under Lambda Function, update the configuration to point to the created alias instead of the Lambda function.
arn:aws:lambda:us-west-1:xxxxxxxx:function:canary-deployment:new-version
  • Then save
  • Under “Actions,” select “Deploy API.”
  • Choose a deployment stage (create a new one if needed). For canary deployments, creating a separate stage (e.g., “canary”) is recommended.
stage ‘test’ was created

4. Monitor and Test the Canary Deployment:

  • After deployment, API Gateway starts routing a portion of traffic (as specified in the weighted alias) to the new Lambda version.
  • Use tools like CloudWatch metrics, logs, or your application’s monitoring setup to closely monitor the performance and health of the canary deployment. This includes error rates, latency, and any other relevant metrics for your application.
  • Thoroughly test the canary deployment by simulating real-world traffic patterns through manual testing or automated tools. Ensure the new version functions as expected.
New Version
Stable Version

In conclusion, weighted alias traffic shifting in AWS Lambda enables smooth canary deployments by gradually routing traffic to a new function version, ensuring a controlled and reliable rollout.

--

--

Responses (1)