Migrating ECS Services from EC2 to Fargate

In this guide, you will:

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:

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 TrafficFlow requests Fargate but the stack has no Fargate resources (for example, LaunchType is still EC2), the listener safely falls back to sending traffic to EC2.

Prerequisites

Step 1: Enable Fargate in TEF

Follow the Updating TEF guide to update the TEF provisioned product, setting:

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 groupRulePurpose
Load balancer SGEgress TCP 8443 to the API service SGALB health checks and traffic to task ENIs
API service SGIngress TCP 8443 from the load balancer SGALB health checks and traffic to task ENIs
Database SGTask 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:

Step 4: Validate the warm standby

  1. Target health: in the AWS Console (EC2 > Target Groups) or CLI, check the -fg target 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.

  2. 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.

  3. 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/health
    

    Use ec2 as the header value to compare against the EC2 path.

Step 5: Flip traffic to Fargate

Either:

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:

  1. Update TEF: Launch Type = Fargate, Traffic Flow = Fargate. Clear any TE-level overrides.
  2. Update the TE stack. The EC2 task definitions and services are removed from the stack.
  3. 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

SymptomLikely 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 taskThe 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 503The 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 EC2The 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.