Skip to content

feat(aws-lambda): CodeDeploy-managed traffic shifting with smoke tests, canary/linear deployments, and alarm-based rollbacks #6719

@anatolzak

Description

@anatolzak

Summary

Add support for controlled traffic shifting when deploying Lambda functions using AWS CodeDeploy. This gives developers the ability to:

  • Validate new versions before any traffic reaches them — run a before-traffic hook that invokes the new version and verifies it works before CodeDeploy shifts traffic
  • Gradually shift traffic — canary (send a percentage for a duration, then 100%) or linear (increment by a percentage at regular intervals)
  • Automatically roll back on failure — attach CloudWatch alarms that trigger a rollback if error rates or latency spike during the rollout

API

const fn = new sst.aws.Function("Api", {
  handler: "src/api.handler",
  url: true,
  rollout: {
    type: "canary",       // or "linear" or "all-at-once"
    percentage: 10,
    duration: "10 minutes",
    alarms: [errorAlarm.name],
    beforeTraffic: "src/before-traffic.handler",
  },
});

The before-traffic hook uses a new SDK:

import { rollout } from "sst/aws/rollout";

export const handler = rollout.handler(async (event) => {
  const resp = await fetch(Resource.Function.latestUrl);
  await rollout.report(event, resp.ok ? "Succeeded" : "Failed");
});

Features

  • Three deployment strategies: all-at-once, canary, linear
  • Before/after traffic hooks: validate new versions before shifting traffic, run post-deployment checks after
  • CloudWatch alarm integration: automatic rollback if alarms fire during deployment
  • SNS notifications: subscribe to deployment lifecycle events (start, success, failure, rollback)
  • Conflict handling: configure what happens when a new deploy starts while a rollout is in progress (cancel, rollback, fail)
  • Function URL support: url points to the stable alias, latestUrl exposes the latest version for testing
  • Works with API Gateway, Router, and other event sources: pass the function directly or use fn.targetArn to route to the stable version
  • SST SDK for lifecycle hooks: rollout.handler() for typed events, rollout.report() to report status — no need to use the CodeDeploy SDK directly
  • addRollout() for deferred configuration: create the alias upfront, configure the deployment strategy later when you need to reference the function in hook functions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions