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


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 15+ 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