Blue/Green with CodeDeploy
Shift traffic to a new version all at once, with instant rollback, using CodeDeploy.
A blue/green deployment runs the new version (green) alongside the old (blue), then shifts traffic over a second target group. AWS CodeDeploy orchestrates this for ECS.
Why blue/green
- Test the green version before sending it real traffic.
- Shift traffic all at once, linearly, or in canary steps.
- Instant rollback β just point traffic back at blue.
Example
{
"version": "0.0",
"Resources": [
{
"TargetService": {
"Type": "AWS::ECS::Service",
"Properties": {
"TaskDefinition": "arn:aws:ecs:us-east-1:123456789012:task-definition/web:9",
"LoadBalancerInfo": {
"ContainerName": "web",
"ContainerPort": 80
}
}
}
}
]
}When to use it
- A team uses CodeDeploy blue/green to shift production traffic from the old ECS service version to the new one all at once, with the ability to roll back instantly.
- A release engineer schedules a canary deployment that sends 10% of traffic to the green environment for 10 minutes before a full traffic shift.
- An SRE configures a CodeDeploy deployment group with a 5-minute bake time before the old (blue) tasks are terminated after a successful shift.
More examples
Create CodeDeploy App for ECS
Creates a CodeDeploy application with the ECS compute platform as the prerequisite for blue/green deployments.
aws deploy create-application \
--application-name api-app \
--compute-platform ECSCreate Blue/Green Deployment Group
Creates a CodeDeploy deployment group for an ECS service with two target groups and an ALB listener for blue/green traffic shifting.
aws deploy create-deployment-group \
--application-name api-app \
--deployment-group-name api-bg \
--deployment-config-name CodeDeployDefault.ECSAllAtOnce \
--service-role-arn arn:aws:iam::123456789012:role/CodeDeployRoleForECS \
--ecs-services clusterName=prod,serviceName=api \
--load-balancer-info \
'targetGroupPairInfoList=[{targetGroups=[{name=blue-tg},{name=green-tg}],prodTrafficRoute={listenerArns=[arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/prod-alb/abc/def]}}]'Trigger a Blue/Green Deployment
Triggers a blue/green deployment that shifts all ALB traffic to a new ECS task definition revision with instant rollback available for 1 hour.
aws deploy create-deployment \
--application-name api-app \
--deployment-group-name api-bg \
--revision \
'revisionType=AppSpecContent,appSpecContent={content="{\"version\":0.0,\"Resources\":[{\"TargetService\":{\"Type\":\"AWS::ECS::Service\",\"Properties\":{\"TaskDefinition\":\"arn:aws:ecs:us-east-1:123456789012:task-definition/api:9\",\"LoadBalancerInfo\":{\"ContainerName\":\"api\",\"ContainerPort\":8080}}}}]}"}'
Discussion