Cloud / IaC · Freelance

ECS Blue/Green Deployments with CodeDeploy and GitHub Actions

CloudFormation stack for ECS on EC2 where CodeDeploy performs blue/green deployments with auto-rollback, driven by GitHub Actions.

  • ECS
  • ALB
  • CodeDeploy
  • Lambda
  • CloudFormation
  • Secrets Manager
  • GitHub Actions
  • ECR

// Overview

The main template provisions the cluster, an ALB with paired target groups per service, ECS services using the CodeDeploy controller, and a database instance with scheduled snapshots. Because CloudFormation lacked native support at the time, an inline Lambda-backed custom resource creates the deployment groups. A separate template builds the network. A GitHub Actions workflow lints, tests, builds, publishes images and triggers deployment.

// The problem

Zero-downtime, rollback-capable releases for containerised services.

// Flow

ECS Blue/Green Deployments with CodeDeploy and GitHub Actions flow1. Lint, test, build → 2. Image publish → 3. Revision file → 4. CodeDeploy → 5. ALB → 6. Rollback → 7. Data layer → 8. Secrets01Lint, test, build02Image publish03Revision file04 · GATECodeDeploy05ALB06Rollback07Data layer08Secrets
  1. Lint, test, buildGitHub Actions on push and pull request
  2. Image publishbuild and push to ECR
  3. Revision fileJSON referencing the new task definition
  4. CodeDeploy gateblue/green per service via a custom-resource deployment group
  5. ALBtwo target groups per service; listener rules switch traffic
  6. Rollbackautomatic on deployment failure
  7. Data layersnapshots on a schedule
  8. SecretsSecrets Manager references passed as parameters, not values

// Design decisions & guardrails

  • A custom-resource Lambda fills a CloudFormation gap.
  • Deploy only from protected branches; environment type is in every name.
  • Secrets referenced by ARN; network in a separate template.

// Outcome

Blue/green releases with automatic rollback on failure.

// Related