AWS CodeDeploy Deployment Strategies - Blue/Green Deployments for EC2, ECS, and Lambda

Manage deployment strategies across three platforms - EC2, ECS, and Lambda - in a unified way. This article covers ECS blue/green deployments and automatic rollback triggered by CloudWatch alarms.

Supported Platforms and Deployment Types

CodeDeploy is a deployment service that supports three platforms: EC2/on-premises, ECS, and Lambda. For EC2/on-premises, you can choose between in-place deployment (sequentially updating existing instances) and blue/green deployment (creating a new instance group and switching over). For ECS, only blue/green deployment is supported, where a new task set is launched and the target group is switched. For Lambda, it provides gradual deployment through traffic shifting. CodeDeploy itself is free for deployments to EC2 and Lambda, with charges applying only for ECS deployments on a per-update basis.

Designing ECS Blue/Green Deployments

In ECS blue/green deployments, two target groups (production and test) are configured on the ALB. When a deployment starts, a new task set (Green) is launched and registered with the test target group. You verify the Green environment's behavior through the test listener port, and if there are no issues, production listener traffic is switched to Green. Even after the switch, the old task set (Blue) is retained for a specified wait time (default 1 hour), allowing immediate rollback if problems occur. The recommended configuration is to run automated tests in the AfterAllowTestTraffic hook of the AppSpec file and automatically roll back on test failure.

Automatic Rollback and Alarm Integration

CodeDeploy's automatic rollback operates on two triggers: deployment failure and CloudWatch alarm activation. With CloudWatch alarm integration, you monitor post-deployment error rates (percentage of 5xx responses), latency (p99 response time), and business metrics (order success rate, etc.), and automatically roll back to the previous version when thresholds are exceeded. For Lambda traffic shifting, Canary deployment routes 10% of traffic to the new version first, and if no alarms fire during the specified period (e.g., 10 minutes), the remaining 90% is switched over. Linear deployment gradually shifts traffic in 10% increments, monitoring alarms at each step. For a comprehensive guide to CodeDeploy pipeline design, refer to technical books on Amazon.

CodeDeploy Pricing

CodeDeploy is free for all deployments to EC2, ECS, and Lambda. While compute costs are incurred during the period when resources temporarily run at double capacity during blue/green deployments, there are no additional charges for CodeDeploy itself. In ECS blue/green deployments, both Blue and Green tasks run simultaneously from the time the Green task set launches until traffic is switched, so Fargate or EC2 charges apply for that period. You can reduce costs by shortening the retention time for the Blue task set after the switch.

Summary

CodeDeploy is a service that provides unified management of safe deployments to EC2, ECS, and Lambda. Blue/green deployments and traffic shifting minimize risk in production environments, and CloudWatch alarm integration enables automatic rollback when issues arise. By incorporating tests into the deployment process through AppSpec lifecycle hooks, you can build deployment automation that ensures quality.