DOP Lab 04 - Canary-deploy a Lambda with automatic rollback

Scaffold: 3/5. The function, its published version and live alias, the Errors alarm, the CodeDeploy application, and a deployment group with the canary config and a service role are all built. You wire the two things that make a bad release safe: the alarm configuration and the auto-rollback configuration on the deployment group.

The scenario

A bad Lambda release used to take down 100% of traffic the instant it shipped. Update the function, and every request hits the new code at once; if the new code throws, everyone gets the error until someone notices and rolls it back by hand.

A canary deployment shifts a slice first. CodeDeploy moves 10% of traffic to the new version, waits five minutes, then shifts the rest (CodeDeployDefault.LambdaCanary10Percent5Minutes). During that five-minute window a CloudWatch alarm watches the new version’s error rate, and if it fires, CodeDeploy rolls the alias back to the old version before the other 90% is ever exposed. The slice is the blast radius, and the alarm is the tripwire.

The catch is that the canary schedule and the tripwire are separate settings. A deployment group can run the canary and still never roll back, because nothing is watching. In this lab the canary config and service role are already in place; what is missing is the alarm and the auto-rollback that turn a slow shift into a safe one.

What’s provided

Deploying src/template.yaml as shipped succeeds, and a canary deployment even runs to completion, but a canary that errored would keep shifting to 100%: nothing is watching the alarm, and nothing rolls back.

Your task

Open src/template.yaml and fill in the TODO block on CanaryDeploymentGroup with two properties:

Together they close the loop: a canary that starts erroring trips the alarm, CodeDeploy stops the deployment on the alarm, and auto-rollback returns the alias to the version that was working.

If you want the mechanics first, read the walk-through post linked at the bottom.

Run it

# Defaults: stack dop-lab-04, region ap-southeast-2.
./scripts/deploy.sh          # deploys src/template.yaml
./scripts/test.sh            # runs a real canary and checks the group config
./scripts/teardown.sh        # deletes everything

test.sh takes about five minutes. It publishes a new (healthy) function version, hands CodeDeploy an AppSpec that shifts the live alias to it, and polls until the canary deployment succeeds — a genuine LambdaCanary10Percent5Minutes shift, 10% first then the rest after the five-minute bake. It then confirms the alias points at the new version, and inspects the deployment group to assert the alarm and auto-rollback are wired.

What success looks like

./scripts/test.sh prints:

  ok: canary completed and the live alias now points at version 2
  ok: deployment group watches the Errors alarm (dop-lab-04-canary-errors)
  ok: deployment group auto-rolls back on DEPLOYMENT_STOP_ON_ALARM
PASS: the canary shifted a healthy release to completion, and the deployment
group is wired to roll back a bad canary automatically (alarm + auto-rollback).

The test never forces a failing canary — a forced rollback is flaky and slow to assert. It proves the mechanics on a healthy change and inspects the group’s config to prove the rollback safety is in place.

If it fails

Reveal the solution

Deploy the complete reference template without editing anything:

SRC=solution ./scripts/deploy.sh && ./scripts/test.sh

What you just learned

Next

The rest of the DOP lab track is listed in labs/README-dop.md.