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
- Cloud Engineer Pillar Guide: AWS ALB, ASG, ECS & Zero-Downtime Deployments
- FinOps Cloud Cost Optimization on AWS & Google Cloud
- 1-Day Engineering Service: EC2-to-ECS Migration & ALB Architecture Blueprint
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.