Skip to main content
EC2 Auto Scaling capacity providers let you run ECS tasks on EC2 instances that you manage. This gives you control over instance types, AMIs, storage, and networking configuration — at the cost of additional operational overhead compared to Fargate.
You cannot mix EC2-based capacity providers with Fargate capacity providers on the same cluster. If you need Fargate, create a separate cluster.

Prerequisites

Before attaching an ASG as a capacity provider, you need an Auto Scaling Group. The terraform-aws-autoscaling module is the recommended way to create one. Key requirements for ASGs used with ECS:
  • The EC2 instances must run the ECS-optimized Amazon Linux 2023 AMI (or equivalent). Retrieve the latest AMI ID from SSM:
  • Instances must have the AmazonEC2ContainerServiceforEC2Role IAM policy attached.
  • User data must register the instance with the correct ECS cluster:
  • The ASG must have protect_from_scale_in = true when using managed_termination_protection = "ENABLED".
  • Tag the ASG with AmazonECSManaged = true to avoid provider issues:

Setting up the Auto Scaling Groups

The ec2-autoscaling example creates two ASGs — one for on-demand instances and one for Spot:

Attaching ASGs as capacity providers

Once the ASGs exist, reference their ARNs in the capacity_providers input of the cluster module. Each entry in the map creates one ECS capacity provider resource.

Managed scaling configuration

Managed scaling allows ECS to automatically adjust the size of the ASG based on the number of tasks waiting to be placed.
Use a lower target_capacity (60–70%) for on-demand capacity providers to leave headroom for bursts. Use a higher value (90%) for Spot providers to maximize cost savings.

Managed draining and termination protection

Managed draining (managed_draining = "ENABLED") instructs ECS to automatically drain tasks from container instances before the ASG terminates them. This ensures graceful task shutdown during scale-in events. Managed termination protection (managed_termination_protection = "ENABLED") prevents the ASG from terminating instances that still have running ECS tasks. ECS removes the scale-in protection only after tasks have been safely moved.
When managed_termination_protection = "ENABLED", the ASG must have protect_from_scale_in = true set in the terraform-aws-autoscaling module. Without this, Terraform will return a validation error.
To disable managed draining (useful for testing or when using custom draining logic):

Deploying a service onto EC2 capacity

Services that target EC2 capacity providers must set requires_compatibilities = ["EC2"] and reference the capacity provider by name:

Attaching EBS volumes

EC2 tasks support attaching managed EBS volumes at launch time: