Autoscaling EC2 Capacity

Scale the underlying instances with an Auto Scaling group and managed cluster scaling.

On the EC2 launch type there are two scaling layers:

  • Service auto scaling — changes how many tasks run.
  • Cluster auto scaling — changes how many EC2 instances exist.

A capacity provider with managed scaling watches a CapacityProviderReservation metric and adjusts the Auto Scaling group so pending tasks always find room.

Example

Example · bash
# Create a capacity provider tied to an Auto Scaling group
aws ecs create-capacity-provider \
  --name ec2-asg \
  --auto-scaling-group-provider 'autoScalingGroupArn=arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:...,managedScaling={status=ENABLED,targetCapacity=100},managedTerminationProtection=ENABLED'

When to use it

  • A team enables ECS managed scaling on their capacity provider so EC2 instances are automatically added when batch job tasks are queued waiting for capacity.
  • An SRE configures managed termination protection so that when Auto Scaling scales in, ECS first drains all tasks off the instance before it is terminated.
  • A FinOps engineer sets the target capacity to 90% on the managed scaling policy to maximise bin-packing and reduce the number of idle EC2 instances.

More examples

Enable Managed Scaling on Capacity Provider

Enables managed scaling on the capacity provider targeting 90% cluster utilization with step sizes between 1 and 5 instances.

Example · bash
aws ecs update-capacity-provider \
  --name my-asg-cp \
  --auto-scaling-group-provider \
    'managedScaling={status=ENABLED,targetCapacity=90,minimumScalingStepSize=1,maximumScalingStepSize=5}'

Scale In with Termination Protection

Enables managed termination protection so ECS drains tasks off an EC2 instance before Auto Scaling is allowed to terminate it.

Example · bash
aws ecs update-capacity-provider \
  --name my-asg-cp \
  --auto-scaling-group-provider \
    'managedTerminationProtection=ENABLED,managedScaling={status=ENABLED,targetCapacity=80}'

Drain a Container Instance Manually

Finds a specific container instance by EC2 instance ID and sets it to DRAINING so ECS migrates its tasks before the host is removed.

Example · bash
INSTANCE_ARN=$(aws ecs list-container-instances \
  --cluster my-ec2-cluster \
  --filter 'ec2InstanceId == i-0abc12345' \
  --query 'containerInstanceArns[0]' --output text)

aws ecs update-container-instances-state \
  --cluster my-ec2-cluster \
  --container-instances "$INSTANCE_ARN" \
  --status DRAINING

Discussion

  • Be the first to comment on this lesson.