Operating Amazon ECS Services and Capacity
This lesson explains how CloudOps engineers configure Amazon ECS services, scaling and capacity on Fargate or EC2 and keep tasks healthy.
Learning outcomes
After completing this lesson, you will be able to:
- Describe task definitions, tasks and services.
- Use launch types and capacity providers.
- Configure service auto scaling.
- Resolve tasks stuck in PROVISIONING.
Task definitions, tasks and services
A task definition describes one or more containers: images, CPU and memory, port mappings, environment variables and secrets (from Secrets Manager or Parameter Store), log configuration, health checks and two IAM roles. The task execution role lets the ECS agent pull images from ECR, fetch secrets and write logs; the task role gives the application permissions to call AWS APIs. A service keeps a desired number of tasks running, replaces failed tasks, registers them with load balancer target groups and spreads them across AZs.
Task role or execution role?
Launch types and capacity providers
With Fargate, AWS manages the underlying servers and each task gets its own isolated environment; you pay for task vCPU and memory. With the EC2 launch type you manage container instances in an Auto Scaling group, which gives more control and can be cheaper at high utilisation. ECS Managed Instances is a third option where AWS provisions and patches EC2 instances for you. Capacity providers attach capacity to a cluster: FARGATE and FARGATE_SPOT, or Auto Scaling group capacity providers with managed scaling (which scales the group to fit tasks, based on the CapacityProviderReservation metric) and managed termination protection. A capacity provider strategy with a base and weights splits tasks, for example a base of 2 on Fargate and the rest weighted 1:3 Fargate to Fargate Spot.
The following diagram shows a service spread across three capacity providers.

Choosing capacity
Service auto scaling
Services scale with Application Auto Scaling on ECSServiceAverageCPUUtilization, ECSServiceAverageMemoryUtilization, ALBRequestCountPerTarget or custom metrics such as SQS backlog per task, using target tracking, step or scheduled policies. On EC2 capacity, service scaling adds tasks and capacity provider managed scaling adds instances when tasks are in the PROVISIONING state because no instance can fit them.
Tasks stuck in PROVISIONING
An ECS service on an EC2 cluster scales from 10 to 40 tasks, but 30 tasks remain in PROVISIONING. The cluster uses an Auto Scaling group added directly, not as a capacity provider, so nothing scales the instances. The operator creates an Auto Scaling group capacity provider with managed scaling at 100% target capacity and managed termination protection, sets it as the cluster's default strategy, and updates the service to use it. The group grows until all tasks are placed and shrinks after scale-in without terminating instances that still run tasks.
Which role lets an ECS task pull its image and write logs?
Show answer
The task execution role. The task role grants the application's own AWS permissions.
Exam Tips
Remember for the exam
- Task execution role: pull images, read secrets, write logs. Task role: the application's AWS permissions.
- Capacity providers with managed scaling scale EC2 capacity to fit tasks.
- Use capacity provider strategies to mix Fargate and Fargate Spot.
- Scale services with Application Auto Scaling on CPU, memory, requests or custom metrics.