Picture a Friday afternoon release. The pull request looked clean, the tests were green, and cdk deploy finished without complaint. Twenty minutes later the support channel lights up, because every single caller of your Lambda function now hits the same bug. A plain deployment swaps the code for everyone at once, so a mistake reaches all users in the same second.
There is a calmer way to ship: let the new version handle only a small slice of real requests first, watch an error metric, and fall back automatically if things go wrong. I walked through a complete setup with AWS CDK in my article Lambda canary deployments with CDK and alarm rollback. Here is the short version of why it works and what to watch out for.
The moving parts behind a canary release
Lambda already ships with everything you need. A version is an immutable snapshot of code plus configuration. An alias such as live is a stable name that points at a version, and it can split invocations between two versions by percentage. Callers only ever talk to the alias, so you can change what sits behind it without anyone noticing.
AWS CodeDeploy then moves the weights for you. With a configuration like LambdaCanary10Percent5Minutes, the new version gets ten percent of the traffic, and after five quiet minutes it receives the rest. Linear variants exist as well, and in CDK you can define your own percentages and intervals. When the CloudFormation stack updates, the alias change is handed to CodeDeploy instead of being applied directly. CDK wires that up for you.
A trimmed-down version of the core wiring looks roughly like this:
const liveAlias = new lambda.Alias(this, 'Live', { aliasName: 'live', version: priceFn.currentVersion, }); new codedeploy.LambdaDeploymentGroup(this, 'Canary', { alias: liveAlias, deploymentConfig: codedeploy.LambdaDeploymentConfig.CANARY_10PERCENT_5MINUTES, alarms: [errorRateAlarm], preHook: preTrafficHook, }); Four lessons that make or break the setup
1. Your API must call the alias, not the function. This is the mistake I see most often. If the integration targets the function itself, CodeDeploy happily shifts weights on an alias nobody uses. The same applies to SQS or Kinesis event source mappings, EventBridge rules and Step Functions.
2. Alarm on an error rate, with a short period. Errors divided by invocations works at low and high traffic alike. The one-minute period has to sit on the math expression itself; otherwise the alarm evaluates over five minutes and only reacts after the canary window has already closed. Also remember that only thrown errors show up in the Errors metric. A handler that swallows everything and returns 500 needs its own metric.
3. A pre-traffic hook catches the obvious breakage first. Before any real user sees the new version, a small Lambda function invokes it directly with a few smoke tests and reports the result back to CodeDeploy. It has to report in every case, or your deployment hangs until CodeDeploy gives up after an hour.
4. Low traffic weakens the signal. If a function only gets a handful of calls per hour, the canary may not see a single request in five minutes. Then the hook carries most of the weight, and a longer canary phase makes sense.
The trade-offs I accept
For every Lambda function sitting directly behind an API for real users, I treat canary releases as mandatory. The extra CDK code is small, and a bug now hits a tenth of the requests for a few minutes instead of everyone until someone notices.
It is not free, though. Every deployment gets at least five minutes longer, which your team should know before the pipeline suddenly feels slow. During the canary phase two versions run side by side, so data format changes need to be backward compatible. And cdk deploy --hotswap bypasses the whole mechanism, so keep it out of production. For nightly batch functions with no traffic to split, good tests and a hook help more than a canary.
What the full article covers
- A complete, runnable CDK stack for a small pricing API, including alias, alarm and deployment group
- The full pre-traffic hook in TypeScript with smoke tests and safe error handling
- Vitest tests for the logic and for the synthesized CloudFormation template
- A deliberately broken release to watch the automatic rollback happen, plus costs, security and an FAQ
Read the whole walkthrough on my site: Lambda canary deployments with CDK and alarm rollback
📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.
👉 Next step: Curious what your own Lambda functions cost? Try my Lambda cost calculator – it runs right in your browser.
📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).
Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en