Blue/Green Deployments with Lambda: A DevOps Guide to Safe Releases
Master Blue/Green and Canary deployments using AWS Lambda aliases. Learn how to shift traffic safely, perform canary releases, and rollback instantly.

Previously in this course, we covered Managing Lambda Versions and Aliases: A Deployment Guide, where we established how to create immutable snapshots of your code. In this lesson, we take that foundation and apply it to real-world production safety by implementing traffic shifting.
In a traditional deployment, you overwrite your code, creating a "big bang" moment where the old code dies and the new code starts. If the new code is buggy, your users feel it instantly. Traffic shifting changes this by allowing you to route a percentage of requests to a new version of your function, giving you a safety net.
Understanding Deployment Strategies
Traffic shifting allows for two primary patterns that significantly reduce risk:
- Blue/Green Deployment: You have two identical environments. "Blue" is currently live. You deploy to "Green." Once verified, you flip a switch to route 100% of traffic to Green. If something breaks, you flip back to Blue.
- Canary Deployment: You route a small percentage (e.g., 5-10%) of traffic to your new version. You monitor logs and metrics. If the error rate stays low, you incrementally increase the percentage until the new version handles all traffic.
How Lambda Traffic Shifting Works
AWS Lambda uses Weighted Aliases to manage this. An alias points to one or two versions of a function, each with a specific weight.
| Feature | Description |
|---|---|
| Primary Version | The current stable version receiving the majority of traffic. |
| Secondary Version | The new version receiving a fractional amount of traffic (the canary). |
| Weight | The percentage (0.0 to 1.0) of traffic directed to the secondary version. |
Worked Example: Shifting Traffic with AWS CDK
To implement this, you must configure a lambda.Alias in your CDK code. This replaces the standard Function reference in your API Gateway or event source.
TYPESCRIPTimport * as lambda from CE9178">'aws-cdk-lib/aws-lambda'; // 1. Define the function const myFunction = new lambda.Function(this, CE9178">'MyFunction', { runtime: lambda.Runtime.NODEJS_18_X, handler: CE9178">'index.handler', code: lambda.Code.fromAsset(CE9178">'lambda'), }); // 2. Create a version const version = myFunction.currentVersion; // 3. Create the alias with a 10% canary const alias = new lambda.Alias(this, CE9178">'MyAlias', { aliasName: CE9178">'prod', version: version, // This is our CE9178">'Blue' (stable) additionalVersions: [ { version: newVersion, // This is our CE9178">'Green' (new) weight: 0.1, // 10% traffic to Green } ], });
When you deploy this stack, 90% of requests go to version, and 10% go to newVersion. You can observe these requests in your CloudWatch Dashboards to ensure the new code isn't throwing errors.
Performing a Rollback
The beauty of this pattern is the rollback. If you discover a bug in the 10% canary traffic, you don't need to redeploy the old code. You simply update the weight property in your CDK code back to 0 and redeploy the stack. Because the old code was never deleted (it's an immutable version), traffic immediately reverts to the stable state.
Hands-on Exercise
- Publish a Version: Using the AWS CLI or Console, publish a new version of your existing Lambda function.
- Create an Alias: Create an alias named
livethat points to this version. - Shift Traffic: Update the alias configuration to point to a different version (or a newer version of the same function) with a 20% weight.
- Verify: Invoke your function 10 times. You should see roughly 8 hits on the old version and 2 on the new one.
Common Pitfalls
- Stateful Functions: If your Lambda writes to a database, ensure your schema changes are backward compatible. Read the guide on Laravel Migrations for Blue-Green Deployments: A Practical Guide to understand why database changes must be decoupled from code changes.
- Dependency Drift: If your new version uses different environment variables, ensure both versions can handle the environment safely.
- Forgetting to Delete: Don't keep old versions around forever; they count toward your storage limit and make auditing confusing.
FAQ
Q: Can I automate the traffic shift? A: Yes. In our upcoming lessons, we will explore using GitHub Actions to automatically update the alias weight after running integration tests.
Q: Does this work with API Gateway? A: Yes, point your API Gateway Integration to the Lambda Alias ARN, not the Function ARN.
Q: Is there a cost for using versions and aliases? A: No, you only pay for the storage of the code versions and the execution of the functions themselves.
Recap
Blue/Green and Canary deployments are the standard for high-availability systems. By using Lambda aliases, you decouple your code versions from your traffic routing, allowing for near-instant rollbacks and safer releases. Always remember: if you can't roll back in under a minute, you aren't ready for production.
Up next: Automating Deployments with CI/CD.
Work with me

CI/CD Pipeline & Docker Containerization
Ship with confidence: automated CI/CD pipelines and Docker setups so every push is tested and deployed โ no more manual, error-prone releases.

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server โ properly configured, hardened, and deployment-ready. No more wrestling with the command line.


