Cloud & Backend Engineer Guide: Strangler-Fig Migration from Monolithic EC2 to AWS ECS & ALB

Attempting a "big-bang" rewrite of a revenue-generating EC2 monolith into 12 microservices almost always stalls for a year while feature delivery halts. Instead, as a cloud engineer and backend engineer, I execute incremental, zero-downtime Strangler-Fig migrations using an AWS Application Load Balancer (ALB) path/host routing layer, Amazon ECS on Fargate/EC2 with awsvpc task networking, and connection-draining lifecycle hooks.

1. ALB Listener Path Routing & Graceful Target Group Deregistration in Terraform

By placing an Application Load Balancer in front of the existing EC2 Auto Scaling Group as the default fallback target group, we can carve out high-load domains (such as /api/v2/webhooks/* or /api/v2/search/*) into independent ECS services with zero DNS cutover risk:

# terraform/alb_strangler_routing.tf
resource "aws_lb_target_group" "ecs_Billing_service" {
  name                 = "prod-ecs-billing-tg"
  port                 = 8080
  protocol             = "HTTP"
  vpc_id               = aws_vpc.main.id
  target_type          = "ip" # Required for ECS awsvpc mode
  deregistration_delay = 30   # Drain in-flight requests for 30s on scale-in

  health_check {
    path                = "/healthz/ready"
    interval            = 15
    timeout             = 5
    healthy_threshold   = 2
    unhealthy_threshold = 3
    matcher             = "200"
  }
}

resource "aws_lb_listener_rule" "strangle_billing_routes" {
  listener_arn = aws_lb_listener.https_443.arn
  priority     = 100

  action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.ecs_Billing_service.arn
  }

  condition {
    path_pattern {
      values = ["/api/v2/billing/*", "/api/v2/invoices/*"]
    }
  }
}

2. Handling SIGTERM and stopTimeout in ECS Task Definitions

When ECS replaces a task during rolling deployment, it sends SIGTERM to PID 1 inside the container after removing the task IP from the ALB target group. Always use exec form in your Dockerfile ENTRYPOINT so your Go/Python server receives SIGTERM directly, stops accepting new keep-alive connections, and flushes in-flight PostgreSQL transactions before stopTimeout expires.

3. Related AWS Cloud & Microservices Engineering Resources

Terraform AWS ALB Weighted Canary Routing & Fargate Drain Configuration

Zero-downtime Strangler-Fig migrations depend on deterministic traffic shifting at the Application Load Balancer listener combined with clean target-group draining during ECS rolling updates:

resource "aws_lb_listener_rule" "strangler_canary" {
  listener_arn = aws_lb_listener.https.arn
  priority     = 40

  action {
    type = "forward"
    forward {
      target_group {
        arn    = aws_lb_target_group.ec2_monolith.arn
        weight = 85
      }
      target_group {
        arn    = aws_lb_target_group.ecs_fargate_service.arn
        weight = 15
      }
      stickiness {
        enabled  = false
        duration = 60
      }
    }
  }

  condition {
    path_pattern {
      values = ["/api/v2/orders/*", "/api/v2/webhooks/*"]
    }
  }
}

resource "aws_lb_target_group" "ecs_fargate_service" {
  name                 = "orders-ecs-fargate-tg"
  port                 = 8080
  protocol             = "HTTP"
  vpc_id               = var.vpc_id
  target_type          = "ip"
  deregistration_delay = 30

  health_check {
    path                = "/healthz"
    healthy_threshold   = 2
    unhealthy_threshold = 2
    interval            = 10
    timeout             = 3
  }
}

EC2-to-ECS Strangler-Fig Migration FAQ

How do you migrate from monolithic EC2 to AWS ECS without downtime?

Place an Application Load Balancer weighted listener rule in front of both targets, keep target_type = "ip" for Fargate tasks, set deregistration_delay = 30 seconds so active requests drain gracefully, and verify p99 errors before shifting weight from 5% to 100%.

Should databases stay shared during an initial EC2-to-ECS extraction?

Yes for early Strangler-Fig slices: keep schemas partitioned by bounded context on Amazon RDS with least-privilege database roles while moving writes through the extracted ECS service API before attempting database-level splits.

What container graceful-shutdown signal settings prevent 502 errors on ECS Fargate?

Configure your Python/Go entrypoint as PID 1 via exec-form Dockerfile directives, catch SIGTERM to reject new readiness probes cleanly, set ECS stopTimeout higher than deregistration_delay, and close idle keep-alive sockets only after in-flight transactions commit.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 25+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs