BACK TO ALL DISPATCHES
Cloud Infrastructure6 min read
August 15, 2026

Architecting Zero-Downtime Blue/Green Deployments with AWS ECS & Terraform

A field-tested production blueprint for deploying containerized microservices using AWS Application Load Balancers, CodeDeploy automated rollback hooks, and immutable Terraform modules.

A
AkhilDevOps & Cloud Infrastructure
#AWS#ECS#Terraform#Docker#DevOps#CI/CD

The Imperative for Zero-Downtime Deployments

In high-concurrency production architectures, even a 30-second restart window or dropped TCP socket can result in thousands of severed user connections and transaction failures. Traditional rolling updates, while simple, often introduce subtle transient errors during API schema migrations or dependency mismatch periods.

To eliminate this volatility, we architected an automated **Blue/Green deployment topology** on AWS Elastic Container Service (Amazon ECS) managed entirely via declarative Terraform modules.


Architectural Design: The Two-Target-Group Model

The core of our immutable deployment strategy relies on AWS Application Load Balancer (ALB) paired with AWS CodeDeploy for container traffic shifting:

[ Global Clients ]
        │ (HTTPS / TLS 1.3)
        ▼
[ Application Load Balancer (Port 443) ]
        │
   ┌────┴─────────────────────────┐
   │ Production Listener          │ Test / Verification Listener
   ▼ (Port 443)                   ▼ (Port 8443)
[ Blue Target Group (Active) ]  [ Green Target Group (Candidate) ]
   │ (Tasks v1.4.0)               │ (Tasks v1.4.1)
   ▼                              ▼
[ ECS Fargate Tasks ]           [ ECS Fargate Tasks ]
  • **Active Traffic (Blue)**: Handles 100% of live production traffic through the primary HTTPS listener (port 443).
  • **Candidate Deployment (Green)**: CodeDeploy provisions the updated container task revision and links it to a dedicated verification test listener (port 8443).
  • **Automated Health & Synthetic Validation**: Synthetic test suites probe port 8443. Only when consecutive health checks and canary requests pass does traffic shifting begin.
  • **Linear / Canary Shifting**: Traffic moves from Blue to Green at 10% per minute or via a fast 5-minute linear ramp.
  • **Instant Rollback Hook**: If CloudWatch alarms register an increase in HTTP 5xx rates (> 0.05%) or latency spikes beyond 120ms P95, CodeDeploy immediately shifts 100% traffic back to Blue with zero user impact.

  • Terraform Implementation Snippet

    Below is the core Terraform configuration establishing the ECS service with the `CODE_DEPLOY` deployment controller:

    hclIMMUTABLE CONFIG
    resource "aws_ecs_service" "api_service" {
      name            = "production-core-api"
      cluster         = aws_ecs_cluster.main.id
      task_definition = aws_ecs_task_definition.app.arn
      desired_count   = 6
      launch_type     = "FARGATE"
    
      deployment_controller {
        type = "CODE_DEPLOY"
      }
    
      network_configuration {
        subnets          = module.vpc.private_subnets
        security_groups  = [aws_security_group.ecs_tasks.id]
        assign_public_ip = false
      }
    
      load_balancer {
        target_group_arn = aws_lb_target_group.blue.arn
        container_name   = "core-api"
        container_port   = 8080
      }
    
      lifecycle {
        ignore_changes = [
          task_definition,
          load_balancer
        ]
      }
    }

    Key Operational Lessons Learned

  • **Database Backward Compatibility**: Always ensure database migrations follow the expand-and-contract pattern. The database schema must simultaneously support both the Blue (v1.4.0) and Green (v1.4.1) codebases during the 10-minute transition window.
  • **Connection Draining (Deregistration Delay)**: Set the deregistration delay on the Blue target group to at least 30–60 seconds so ongoing HTTP Keep-Alive requests gracefully terminate before tasks receive the `SIGTERM` signal.
  • **Zero Drift Automation**: By parameterizing task definitions and deployment group ARNs in reusable CI/CD pipeline stages, developers can push code with full confidence that infrastructure never drifts between staging and production.
  • ABOUT THE AUTHOR

    Akhil — DevOps Engineer

    Specializing in AWS cloud infrastructure, Kubernetes container meshes, zero-downtime CI/CD automation, and Generative AI model inference clusters.