Lesson 2 of 3 Containers on Amazon ECS and Amazon EKS

Scaling Amazon ECS Services and Clusters

Scaling ECS involves two layers: the number of tasks in a service and, on EC2 capacity, the number of instances in the cluster. This lesson explains ECS service auto scaling with Application Auto Scaling, cluster auto scaling with capacity provider managed scaling, task placement on EC2, and how the two layers interact so that tasks are not left pending for lack of capacity.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain ECS service auto scaling with target tracking, step, scheduled and predictive policies.
  • Explain how capacity provider managed scaling uses the CapacityProviderReservation metric to scale EC2 capacity.
  • Describe task placement strategies and constraints for the EC2 launch type.
  • Diagnose tasks stuck in PROVISIONING or PENDING and services that do not scale.

Service Auto Scaling

ECS service auto scaling uses Application Auto Scaling to change a service's desired count. The service is registered as a scalable target with a minimum and maximum task count, and policies adjust the count. Target tracking is the usual choice, with predefined metrics for average service CPU utilisation, average memory utilisation, or ALB request count per target, or a custom metric such as queue depth per task. Step scaling, scheduled scaling and predictive scaling are also available.

The metric must reflect what limits the service. CPU suits compute-bound services; request count per target suits web services where latency rises with concurrency; queue backlog per task suits workers. Scale-in should be conservative so that brief dips do not remove capacity needed moments later, and task scale-in protection lets a task that is processing a long job protect itself from termination through the ECS agent endpoint.

aws application-autoscaling register-scalable-target --service-namespace ecs \
  --resource-id service/prod-cluster/orders-api --scalable-dimension ecs:service:DesiredCount \
  --min-capacity 4 --max-capacity 60

aws application-autoscaling put-scaling-policy --service-namespace ecs \
  --resource-id service/prod-cluster/orders-api --scalable-dimension ecs:service:DesiredCount \
  --policy-name orders-requests --policy-type TargetTrackingScaling \
  --target-tracking-scaling-policy-configuration '{"TargetValue":800,
    "PredefinedMetricSpecification":{"PredefinedMetricType":"ALBRequestCountPerTarget",
      "ResourceLabel":"app/prod-alb/1234567890abcdef/targetgroup/orders/abcdef1234567890"},
    "ScaleInCooldown":300,"ScaleOutCooldown":60}'

Cluster Auto Scaling with Capacity Providers

On Fargate, there is no cluster capacity to manage: each new task gets its own capacity. On EC2, new tasks need room on container instances. Managed scaling on an Auto Scaling group capacity provider solves this. ECS publishes a metric named CapacityProviderReservation, the percentage of the group's capacity that running and pending tasks need, and creates a target tracking policy on the Auto Scaling group to keep it at the configured target capacity. A target of 100% packs instances fully; a lower target, such as 80%, keeps spare capacity so that new tasks start without waiting for instances. When tasks cannot be placed, they wait in the PROVISIONING state while managed scaling adds instances.

Managed termination protection sets scale-in protection on instances that run tasks, so the Auto Scaling group removes only empty instances. Managed instance draining drains tasks from instances that are being terminated, for example during an instance refresh, so that services reschedule tasks gracefully.

LayerMechanismMetric
Tasks in a serviceApplication Auto Scaling policiesCPU, memory, ALBRequestCountPerTarget or custom
EC2 instances in the clusterCapacity provider managed scalingCapacityProviderReservation
Fargate capacityNone neededNot applicable

The following diagram compares two managed scaling target capacities.

Managed scaling target capacity on an EC2 capacity provider. ECS publishes CapacityProviderReservation, the share of the Auto Scaling group's capacity that running and pending tasks need, and target tracking keeps it at the target. At 100% instances are packed full, so a new task waits in PROVISIONING until an instance launches. At a lower target, such as 80%, spare capacity lets new tasks start at once while instances are added in the background. Managed termination protection lets the group remove only empty instances.
Slot counts are illustrative.
Scenario questions

Scale the ECS cluster

Exam skills 3.2.3 · 4.3.1

Practise ECS service and cluster scaling settings.

Log in to try this activity Interactive activities are free with an account.

Task Placement on EC2

With EC2 capacity, ECS decides which instance runs each task using placement strategies and constraints. Strategies are binpack (place tasks on the instance with the least remaining CPU or memory, minimising instances and cost), spread (distribute tasks evenly across a field such as Availability Zone or instance ID, maximising availability) and random. Strategies can be combined, for example spreading by AZ and then binpacking by memory. Constraints restrict placement: distinctInstance places each task on a different instance, and memberOf uses a cluster query language expression, for example requiring an instance type family or a custom attribute. Fargate spreads tasks across AZs automatically.

Sort into groups

Place the tasks

Exam skills 3.2.3 · 4.3.1

Choose the ECS placement strategy or constraint for each requirement.

Log in to try this activity Interactive activities are free with an account.

Diagnosing Scaling Problems

Services that do not scale or tasks that do not start show characteristic symptoms. The table below links the most common ones to causes.

SymptomLikely causeRemediation
Desired count does not increase under loadScalable target maximum reached, or the policy metric does not reflect the bottleneckRaise maximum; choose a better metric
Tasks stay in PROVISIONINGNo EC2 capacity and managed scaling not enabled or at the group maximumEnable managed scaling; raise the Auto Scaling group maximum
Tasks fail with RESOURCE:MEMORY or RESOURCE:CPUNo instance has enough free resources for the task sizeLarger instances, smaller tasks or more capacity
Tasks fail with insufficient IP addressesawsvpc tasks exhausted subnet addressesLarger subnets or additional subnets
Tasks start then stop repeatedlyApplication error, failing health checks or image pull errorsCheck stopped reason, exit codes and logs (see the troubleshooting module)
Fargate tasks fail to launch in burstsFargate quotas or launch rate limitsQuota increase; spread across AZs
Worked example

Scaling an EC2-Backed Cluster

A video processing service runs on an EC2 capacity provider with GPU instances. During spikes, new tasks wait for 10 minutes before starting, and at night, instances with no tasks remain running.

The capacity provider's managed scaling target capacity is set to 100%, so there is never spare room and every new task waits for an instance to launch. Lowering it to 75% keeps a buffer of capacity, so tasks start immediately while managed scaling adds instances in the background. The night-time idle instances are kept because the Auto Scaling group minimum is too high and managed termination protection is not enabled; reducing the minimum and enabling managed termination protection lets the group remove empty instances safely. A warm pool for the GPU instances shortens instance start-up further.

Check yourself

Which placement strategy minimises the number of EC2 instances used by a cluster, and which maximises resilience to an instance or AZ failure?

Show answer

binpack minimises the number of instances by filling each one before using another; spread across Availability Zones (and instances) maximises resilience.

Exam Tips

Remember for the exam

  • ECS service auto scaling uses Application Auto Scaling; target tracking on CPU, memory or ALBRequestCountPerTarget is typical.
  • Capacity provider managed scaling targets CapacityProviderReservation; a target below 100% keeps spare capacity so tasks start quickly.
  • Managed termination protection prevents scale-in of instances with running tasks; tasks waiting for capacity are in PROVISIONING.
  • binpack minimises instances; spread maximises availability; constraints such as distinctInstance and memberOf restrict placement.