AWS Builder Center

Stop Deploying Lambda Functions All at Once: A Practical Guide to Canary Deployments

In serverless development, deploying new code should not mean risking your entire application. A seemingly small change to an AWS Lambda function can introduce unexpected errors, break integrations, or affect production users. One way to minimize this risk is through Canary Deployments.

s a Canary Deployment?
A canary deployment gradually introduces a new application version to production instead of replacing the existing version immediately.
For example:
  1. Deploy the new Lambda version.
  1. Route 10% of incoming traffic to the new version.
  1. Monitor errors and application performance.
  1. If everything works, gradually route 100% of traffic to the new version.
  1. If errors occur, roll back to the previous stable version.
This approach allows developers to detect problems before they affect all users.

Implementing Canary Deployments with AWS

AWS provides several services that make this possible:
  • AWS Lambda Versions and Aliases – Maintain immutable versions and control which version receives production traffic.
  • AWS CodeDeploy – Automate gradual traffic shifting and deployment rollbacks.
  • Amazon CloudWatch – Monitor errors and trigger alarms when deployments introduce problems.
  • AWS SAM – Simplify serverless infrastructure configuration and integrate deployment strategies.
Here's a simplified AWS SAM configuration:
Resources:
QueryFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: src/
AutoPublishAlias: live

DeploymentPreference:
Type: Canary10Percent5Minutes
Alarms:
- !Ref LambdaErrorAlarm
This configuration automatically publishes a new Lambda version and uses CodeDeploy to shift 10% of traffic to it before completing the deployment five minutes later.
If a configured CloudWatch alarm is triggered, the deployment can automatically roll back.

Why Does This Matter?

Canary deployments provide several practical benefits:
  • Reduced deployment risk: New code is exposed to only a small percentage of production traffic initially.
  • Faster recovery: Failed deployments can automatically return to a previously stable version.
  • Improved reliability: Developers can monitor real production behavior before completing a rollout.
  • Safer continuous delivery: Teams can deploy frequently without treating every release as a high-risk event.

Final Thoughts

A successful CI/CD pipeline should do more than automate code delivery. It should also protect the application during deployment.
For developers building production-ready serverless applications, combining Lambda aliases, CodeDeploy, CloudWatch alarms, and AWS SAM is a practical way to achieve safer releases.
Remember: A deployment isn't truly automated if it doesn't have a strategy for handling failure.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article