Q: How do you implement blue-green deployments on ECS?
To implement blue-green deployments on ECS, run the new task set (green) beside the live one (blue) behind the same Application Load Balancer, test it on a separate listener, then shift production traffic and keep blue for fast.
Want to master this scenario in a live sandbox? Stephane Maarek's AWS Certified DevOps Engineer Professional Masterclass on Udemy covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>implement blue-green deployments on ECS</strong>, run the new task set (green) beside the live one (blue) behind the same Application Load Balancer, test it on a separate listener, then shift production traffic and keep blue for fast rollback. The classic route uses the CodeDeploy deployment controller with two target groups; AWS has also added a built-in ECS blue/green strategy, so check current ECS documentation for which you should choose. Interviewers want to see traffic shifting, health validation, rollback triggers and database compatibility.
Architecture: two task sets, two target groups, two listeners
The ALB is the switch; ECS runs both versions at once during the deployment.
- Blue and green: blue is the current task set serving production. Green is the new task definition revision launched in the same service with no production traffic yet.
- Target groups: two target groups (for example
app-blue-tgandapp-green-tg) are attached to the ALB. CodeDeploy swaps which one the production listener forwards to. - Listeners: a production listener (443) serves users. An optional test listener on a different port (for example 8443) lets you validate green before any user sees it.
- Deployment controller: the ECS service must be created with
CODE_DEPLOYas its controller type. It cannot be switched from rolling update afterwards without recreating the service. - Why choose it: blue-green gives you pre-production validation on real infrastructure and a near-instant rollback, at the price of running two copies briefly. A plain rolling update replaces tasks in place and rolls back more slowly.
Hands-on: implement blue-green deployments on ECS with CodeDeploy
Create the service with the CodeDeploy controller, then describe each release with an AppSpec file.
aws ecs create-service --cluster prod --service-name my-app \
--task-definition my-app:12 --desired-count 3 --launch-type FARGATE \
--deployment-controller type=CODE_DEPLOY \
--load-balancers targetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/app-blue-tg/abc,containerName=app,containerPort=8080 \
--network-configuration 'awsvpcConfiguration={subnets=[subnet-aaa,subnet-bbb],securityGroups=[sg-123]}'
# appspec.json
# {"version":"0.0","Resources":[{"TargetService":{"Type":"AWS::ECS::Service",
# "Properties":{"TaskDefinition":"arn:aws:ecs:us-east-1:123456789012:task-definition/my-app:13",
# "LoadBalancerInfo":{"ContainerName":"app","ContainerPort":8080}}}}]}
- Service creation: pass
--deployment-controller type=CODE_DEPLOYand attach the blue target group in--load-balancers. - CodeDeploy application: create an application with compute platform
ECSand a deployment group that names both target groups, the production listener, the optional test listener and a service role. - AppSpec: specifies the new task definition ARN and the container name and port that receive traffic.
- Deploy: start the deployment with
aws deploy create-deployment, passing the AppSpec as the revision.
Traffic shifting, test traffic and lifecycle hooks
The value of blue-green is controlled exposure, not just a faster swap.
aws deploy create-deployment \
--application-name my-app \
--deployment-group-name prod \
--deployment-config-name CodeDeployDefault.ECSCanary10Percent5Minutes \
--revision '{"revisionType":"String","string":{"content":"<appspec JSON>"}}'
aws deploy get-deployment --deployment-id d-XXXXXXXXX \
--query 'deploymentInfo.[status,errorInformation]'
- Shift strategy: choose a predefined configuration such as
CodeDeployDefault.ECSAllAtOnce, a canary (for example 10 percent then the rest after a delay) or linear (equal increments over time). - Test listener: after green is healthy, CodeDeploy routes test traffic. Run smoke tests against the test port before production traffic moves.
- Hooks: AppSpec hooks such as
BeforeAllowTrafficandAfterAllowTestTrafficinvoke Lambda functions that run validation and can fail the deployment. - Termination wait: keep blue running for a configured period after the shift, so rollback is just re-pointing the listener, with no task launch time.
Rollback triggers and failure modes
Define what failure looks like before the deployment starts.
- Automatic rollback: configure the deployment group to roll back on deployment failure and on CloudWatch alarm breach, such as ALB
HTTPCode_Target_5XX_Countor target response time. - Health checks: a weak health endpoint passes a broken build. Use a readiness endpoint that checks critical dependencies, with sensible deregistration delay.
- Database changes: blue and green run simultaneously, so schema migrations must be backward compatible (expand, migrate, then contract in a later release).
- Capacity and quotas: double task counts during a deployment can hit Fargate or ENI limits; check current quotas and request increases ahead of time.
- Stuck deployments: if green tasks never become healthy, the deployment waits and eventually fails. Check the target group health reason and the task's stopped reason with
aws ecs describe-tasks, then fix the image, port or security group.
Verification and automation
Prove each deployment and keep the process in code.
- Verify:
aws ecs describe-servicesshows task sets with statusPRIMARYandACTIVE; confirm running count equals desired on the new set. - Pipeline: build, push to ECR, register a new task definition revision, render AppSpec, then call CodeDeploy from GitHub Actions or CodePipeline.
- Infrastructure as code: define target groups, listeners, CodeDeploy application and alarms in Terraform or CDK so environments match.
- Alternatives: ECS has a built-in blue/green option and a rolling update controller; rolling is simpler when you do not need pre-production validation or instant rollback.
- Blue-green on ECS runs the new task set beside the live one behind the same ALB, using two target groups and a test listener, with CodeDeploy flipping production traffic.
- I validate on the test port and with lifecycle hook Lambdas, and use canary or linear shifting with alarm-based automatic rollback.
- Pipelines register the task definition, render the AppSpec and call CodeDeploy, and migrations stay backward compatible so blue can always take traffic back.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Creating the ECS service with the default rolling controller You cannot convert it to <code>CODE_DEPLOY</code> later; the service has to be recreated.
- Shipping a destructive schema migration with the release Blue still runs against the same database during the shift and rollback, so incompatible changes break it.
- Terminating blue immediately after the traffic shift You lose the instant-rollback advantage; use a termination wait time.
- Using a trivial <code>/health</code> endpoint A build with a broken dependency still passes, so the shift proceeds and fails in production.