Skip to main content

How to do blue-green deployments on AWS with CodeDeploy

Run zero-downtime blue-green deployments for ECS containers using AWS CodeDeploy, two target groups, canary or linear traffic shifting, and alarm-based rollback.

Difficulty
Advanced
Duration
55 minutes
Steps
6

Blue-green deployment runs the new version (green) alongside the old (blue), shifts traffic over once green is healthy, and keeps blue ready for instant rollback. On AWS, CodeDeploy automates this for Amazon ECS by managing two target groups behind an Application Load Balancer.

Prerequisites

  • An AWS account and a containerized app running on ECS (Fargate).
  • An Application Load Balancer.

Steps

1. Prepare two target groups

Blue-green needs two target groups, one for live traffic and one for the new version. The listener points at whichever is current.

2. Configure the ECS service

Set the ECS service's deployment controller to CODE_DEPLOY so CodeDeploy, not ECS rolling update, manages task sets:

deployment_controller { type = "CODE_DEPLOY" }

3. Define the deployment group

Create a CodeDeploy application and deployment group referencing the ECS service, both target groups, and the production listener. CodeDeploy uses these to swap traffic.

4. Choose a traffic shift

Pick a strategy: AllAtOnce, Linear (for example 10% every minute), or Canary (a small percentage, then the rest). Canary catches problems with minimal blast radius.

5. Deploy a new version

Provide an AppSpec that names the new task definition and container/port. Start the deployment:

aws deploy create-deployment --application-name app --deployment-group-name app-dg \
  --revision <appspec-revision>

CodeDeploy launches green tasks, registers them in the spare target group, and shifts traffic per your strategy.

6. Roll back on failure

Attach CloudWatch alarms (error rate, latency) to the deployment group. If an alarm fires during the shift, CodeDeploy automatically rolls back to blue.

Verification

Watch the deployment in the CodeDeploy console as traffic shifts from blue to green. Confirm zero failed requests during the cutover with load running. Trigger an alarm mid-deploy and confirm CodeDeploy reverts to the original task set.

Next Steps

Add a manual approval (test traffic) hook to validate green before production shift, integrate with your CI pipeline, and set a generous bake time before terminating blue.

Prerequisites

  • AWS account
  • A containerized app on ECS
  • An Application Load Balancer

Steps

  • 1
    Prepare two target groups
  • 2
    Configure the ECS service
  • 3
    Define the deployment group
  • 4
    Choose a traffic shift
  • 5
    Deploy a new version
  • 6
    Roll back on failure

Category

Deployment