Capacity Providers

Capacity providers let ECS manage and scale the compute behind your tasks.

A capacity provider tells a cluster where to get compute. For EC2 it links to an Auto Scaling group; for serverless it can be FARGATE or FARGATE_SPOT.

Cluster auto scaling

With an EC2 capacity provider and managed scaling, ECS automatically adds or removes instances to fit pending tasks — so you scale the fleet by scaling tasks.

Strategies

A strategy mixes providers by weight, e.g. mostly Spot with a small on-demand base.

Example

Example · json
{
  "capacityProviders": [
    "ec2-asg",
    "FARGATE_SPOT"
  ],
  "defaultCapacityProviderStrategy": [
    {
      "capacityProvider": "ec2-asg",
      "weight": 1,
      "base": 2
    },
    {
      "capacityProvider": "FARGATE_SPOT",
      "weight": 4
    }
  ]
}

When to use it

  • A team attaches an Auto Scaling group as an ECS capacity provider so ECS automatically adds EC2 instances when tasks cannot be scheduled.
  • A platform engineer sets a mixed capacity provider strategy to run 80% of tasks on Spot EC2 and 20% on On-Demand for cost savings with resilience.
  • An SRE team sets managed termination protection on the capacity provider so ECS drains tasks before Auto Scaling terminates an EC2 instance.

More examples

Create an ASG-Backed Capacity Provider

Creates a capacity provider backed by an Auto Scaling group with managed scaling targeting 80% utilization and termination protection enabled.

Example · bash
aws ecs create-capacity-provider \
  --name my-asg-cp \
  --auto-scaling-group-provider \
    autoScalingGroupArn=arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:abc:autoScalingGroupName/my-asg,\
managedScaling='{status=ENABLED,targetCapacity=80}',\
managedTerminationProtection=ENABLED

Attach Capacity Provider to Cluster

Attaches the ASG capacity provider and Fargate Spot to the cluster with an 80/20 default strategy favouring EC2.

Example · bash
aws ecs put-cluster-capacity-providers \
  --cluster my-ec2-cluster \
  --capacity-providers my-asg-cp FARGATE_SPOT \
  --default-capacity-provider-strategy \
    '[{"capacityProvider":"my-asg-cp","weight":4},{"capacityProvider":"FARGATE_SPOT","weight":1}]'

Use Capacity Provider in Service

Creates a service with a base of 2 tasks guaranteed on the ASG capacity provider, with all additional tasks also placed there.

Example · bash
aws ecs create-service \
  --cluster my-ec2-cluster \
  --service-name worker-svc \
  --task-definition worker:3 \
  --desired-count 10 \
  --capacity-provider-strategy \
    '[{"capacityProvider":"my-asg-cp","weight":1,"base":2}]'

Discussion

  • Be the first to comment on this lesson.