Migrating ECS Services from EC2 to Fargate
In this guide, you will:
- Enable Fargate task definitions and services alongside your existing EC2 deployment (warm standby).
- Validate the Fargate services with zero impact to production traffic.
- Flip traffic from EC2 to Fargate with no downtime, and roll back just as easily.
- Optionally retire the EC2 side once Fargate is fully adopted.
Starting with TE v5.59.0 and TEF v1.71.0, the Guardrails API, Events, Maintenance, and Factory ECS services can run on AWS Fargate instead of (or alongside) EC2 container instances. Running on Fargate removes the need to maintain, patch, and refresh EC2 container instance AMIs.
How it works
Two settings control the migration, both defined in TEF and stored as SSM parameters:
LaunchType(/turbot/enterprise/launch_type) controls which task definitions and services exist in each TE stack. Values:EC2,EC2 and Fargate,Fargate.TrafficFlow(/turbot/enterprise/traffic_flow) controls where the load balancer sends traffic. Values:EC2orFargate. The ALB listener uses weighted forwarding: the active launch type's target groups get weight 100, the other gets 0. Because both sets of services can run simultaneously, the switch is a zero-downtime weight change, not a redeployment.
Fargate task sizing and CPU architecture are also TEF-managed SSM parameters (/turbot/enterprise/api_fargate_cpu, /turbot/enterprise/api_fargate_memory, and equivalent pairs for events, maintenance, and factory, plus /turbot/enterprise/fargate_cpu_architecture, which defaults to ARM64).
The TE stack reads these SSM parameters when it is created or updated. Changing a value in TEF has no effect on a running TE stack until that TE stack is next updated.
The TE stack also has its own optional TrafficFlow (and LaunchType) override parameters. When left empty (the default), the TE stack inherits the TEF values. Setting an override applies to that TE stack only. This is useful during blue-green deployments: a new TE version can receive Fargate traffic while an older version stack stays on EC2.
[!NOTE] If
TrafficFlowrequests Fargate but the stack has no Fargate resources (for example,LaunchTypeis stillEC2), the listener safely falls back to sending traffic to EC2.
Prerequisites
- Access to the Guardrails AWS account with Administrator Privileges.
- Familiarity with AWS Console, Service Catalog, and CloudFormation services.
- TEF version 1.71.0 or higher.
- TE version 5.59.0 or higher.
Step 1: Enable Fargate in TEF
Follow the Updating TEF guide to update the TEF provisioned product, setting:
- Launch Type:
EC2 and Fargate - Traffic Flow:
EC2(leave traffic on your existing EC2 services during validation)
This provisions the Fargate SSM parameters and, for TEF-managed security groups, the security group rules the Fargate services require.
Step 2: Update the TE stack
Update your TE stack (a stack update with unchanged parameters is sufficient) so it picks up the new TEF SSM values. The stack creates Fargate task definitions and services (named with an _fg suffix, for example turbot_5_59_0_api_fg) and registers them with dedicated Fargate target groups (-fg suffix) on the existing load balancer.
The Fargate services now run in warm standby: the ALB health-checks them continuously, but sends them no traffic.
Step 3: Verify the network path
EC2 tasks use bridge networking: traffic reaches the container instance on a dynamic host port (32768-65535) that maps to container port 8443. Fargate tasks use awsvpc networking: each task gets its own ENI, and the ALB connects to the task directly on port 8443. Security group rules written for the dynamic port range do not cover Fargate.
The Fargate task ENIs are attached to exactly three security groups: the API service security group, the outbound internet security group, and the database security group. Rules that exist only on your EC2 container instance security groups do not apply to Fargate tasks.
Verify the following rules exist:
| Security group | Rule | Purpose |
|---|---|---|
| Load balancer SG | Egress TCP 8443 to the API service SG | ALB health checks and traffic to task ENIs |
| API service SG | Ingress TCP 8443 from the load balancer SG | ALB health checks and traffic to task ENIs |
| Database SG | Task ENIs reachable on TCP 5432 (Postgres), 6379 (cache), 6432 (PgBouncer) | Application data path |
[!CAUTION] If your installation uses customer-managed security groups (common with installs into an existing VPC), TEF cannot add the port 8443 rules for you. Add them to your own stack before deploying the Fargate services. Without them, every ALB health check is blocked, and ECS will kill each Fargate task roughly two minutes after launch (
ServiceSchedulerInitiated, exit code 137, "Task failed ELB health checks").
Additional network considerations:
- Proxy environments: the task ENI pulls the container image and ships logs itself (the EC2 container instance no longer does this on its behalf). If outbound traffic is routed through a proxy, create VPC endpoints for ECR API, ECR DKR, S3, and CloudWatch Logs.
- CPU architecture: Fargate tasks default to ARM64 (
/turbot/enterprise/fargate_cpu_architecture). The Guardrails image is multi-architecture; only change this if your organization requires x86_64.
Step 4: Validate the warm standby
-
Target health: in the AWS Console (EC2 > Target Groups) or CLI, check the
-fgtarget groups:aws elbv2 describe-target-health --target-group-arn <https-fg target group ARN>All targets should reach
healthy. The health check is HTTPS on port 8443 against/api/latest/turbot/health, which also exercises database and SQS/SNS connectivity, so a healthy target is a strong signal. -
Logs: Fargate tasks log to the same CloudWatch log groups as the EC2 containers, under stream names prefixed
api/(or the equivalent service prefix) followed by the task ID. Confirm a clean startup. -
Pin a test request to Fargate: the listener includes header-based pin rules that route an individual request to a specific launch type regardless of the traffic flow setting:
curl -H "x-turbot-pin-launch-type: fargate" https://<your-guardrails-host>/api/latest/turbot/healthUse
ec2as the header value to compare against the EC2 path.
Step 5: Flip traffic to Fargate
Either:
- Per TE stack (recommended first): update the TE stack and set its
TrafficFlowoverride parameter toFargate. Only the listener weights change; the update is fast and does not touch the services. - Globally in TEF: update TEF with Traffic Flow =
Fargate, then update each TE stack so it re-reads the SSM parameter. Remember that changing the SSM value alone does not affect a running TE stack.
To roll back, set the value back to EC2 and update the TE stack again. The EC2 services are still running and healthy, so rollback is also just a weight change.
Step 6: Retire the EC2 services (optional)
After a comfortable bake period on Fargate:
- Update TEF: Launch Type =
Fargate, Traffic Flow =Fargate. Clear any TE-level overrides. - Update the TE stack. The EC2 task definitions and services are removed from the stack.
- Decommission the EC2 container instances and their Auto Scaling group per your normal process.
Keep EC2 and Fargate if you prefer to retain the EC2 side as a standby.
Troubleshooting
| Symptom | Likely cause |
|---|---|
Fargate tasks stop ~2 minutes after start with exit code 137, ServiceSchedulerInitiated, "Task failed ELB health checks" | ALB cannot reach the task on port 8443. Verify the security group rules in Step 3 (most commonly missing on customer-managed security groups). |
No api/-prefixed log stream appears for a Fargate task | The container never started: usually an image pull failure. Verify outbound HTTPS from the task subnets and, in proxy environments, the ECR/S3/CloudWatch Logs VPC endpoints. |
| Targets report health check failures with HTTP 503 | The application is running but /api/latest/turbot/health is failing, typically because SQS or SNS is unreachable from the task ENI. Verify outbound connectivity (including any VPC endpoint security groups) from the Fargate task security groups. |
| Traffic flow set to Fargate but traffic still goes to EC2 | The stack has no Fargate resources (check LaunchType), or the TE stack has not been updated since the TEF change. |
If issues persist, open a support ticket with the output of aws elbv2 describe-target-health for the -fg target groups and aws ecs describe-tasks for a recently stopped task.