Skip to Content

ECS scaling and patterns

ECS Service Auto Scaling, scaling the EC2 capacity underneath with Auto Scaling groups and capacity providers, and event-driven ECS patterns with EventBridge and SQS.

ECS Service Auto Scaling

ECS Service Auto Scaling automatically increases or decreases the desired number of ECS tasks in a service.

  • It uses Application Auto Scaling, on metrics such as:
    • the service's average CPU utilization;
    • the service's average memory utilization (scale on RAM);
    • ALB request count per target, a metric from the load balancer.
  • Policy types:
    • target tracking: keep a CloudWatch metric near a target value;
    • step scaling: scale in steps when a CloudWatch alarm fires;
    • scheduled scaling: scale at set dates and times, for predictable changes.
  • ECS Service Auto Scaling (task level) ≠ EC2 Auto Scaling (instance level).
  • Fargate auto scaling is much easier to set up, because it's serverless: there's no instance level.

Scaling the EC2 capacity

Two levels of ECS scaling: when the service's average CPU goes above the target, a CloudWatch alarm makes ECS Service Auto Scaling add tasks; on the EC2 launch type, a capacity provider then adds EC2 instances to the Auto Scaling group when there is not enough capacity Two levels of ECS scaling: when the service's average CPU goes above the target, a CloudWatch alarm makes ECS Service Auto Scaling add tasks; on the EC2 launch type, a capacity provider then adds EC2 instances to the Auto Scaling group when there is not enough capacity

More tasks first, then more instances to hold them.

On the EC2 launch type, new tasks need room. Add EC2 instances to fit them:

  • Auto Scaling group scaling: scale the ASG on CPU utilization, adding instances over time. Simple, but it doesn't know what ECS needs.
  • ECS capacity provider (better):
    • automatically provisions and scales the infrastructure for the ECS tasks;
    • a capacity provider is paired with an Auto Scaling group;
    • it adds EC2 instances when ECS is missing capacity (CPU, RAM…) to place tasks.

Capacity providers also exist for Fargate, Fargate Spot and ECS Managed Instances. A capacity provider strategy spreads tasks across them, for example a base on Fargate and extra tasks on Fargate Spot.

Event-driven patterns

ECS tasks run by EventBridge

An event-driven ECS task: a client uploads a file to Amazon S3; the object-created event reaches EventBridge, whose rule runs a new Fargate task; the task, using its task role, reads the object from S3 and saves the result to DynamoDB An event-driven ECS task: a client uploads a file to Amazon S3; the object-created event reaches EventBridge, whose rule runs a new Fargate task; the task, using its task role, reads the object from S3 and saves the result to DynamoDB

An upload starts a container that does the work.
  1. A client uploads an object to an S3 bucket.
  2. S3 sends an event to EventBridge, where a rule runs an ECS task (on Fargate, in the customer's VPC).
  3. The new task, with a task role allowing S3 and DynamoDB, gets the object, processes it and saves the result to DynamoDB.

ECS tasks on an EventBridge schedule

An EventBridge schedule (for example every hour) runs an ECS task on Fargate that does batch processing, such as working through files in S3. Billing applies only while the task runs.

Queue workers and stopped tasks

Two ECS patterns: queue workers, where an ECS service polls an SQS queue and scales its tasks on the queue length; and alerting on stopped tasks, where an ECS task state change to STOPPED is sent through EventBridge to an SNS email for an administrator Two ECS patterns: queue workers, where an ECS service polls an SQS queue and scales its tasks on the queue length; and alerting on stopped tasks, where an ECS task state change to STOPPED is sent through EventBridge to an SNS email for an administrator

Workers on a queue, and an alert when a task stops.
  • SQS queue example: an ECS service's tasks poll an SQS queue for messages, and ECS Service Auto Scaling adds tasks as the queue grows.
  • Intercept stopped tasks: ECS sends task state changes to EventBridge. A rule matching tasks whose lastStatus is STOPPED (for example Essential container in task exited) triggers an SNS email to an administrator.

Real-world examples

  • A video platform keeps its web service at 60% average CPU with target tracking; a capacity provider adds EC2 instances when tasks have nowhere to go.
  • A document service starts a Fargate task for each uploaded PDF through EventBridge, extracting text into DynamoDB.
  • A nightly job runs a container on an EventBridge schedule at 01:00 instead of keeping a server running all day.

Exam tips

  • "Scale the number of ECS tasks on CPU, memory or ALB requests" → ECS Service Auto Scaling.
  • "Add EC2 instances automatically when tasks can't be placed" → ECS capacity provider with an Auto Scaling group.
  • "Run a container when a file lands in S3" → S3 event → EventBridge rule → ECS task.
  • "Run a container every hour" → EventBridge schedule → ECS task (Fargate).
  • "Process messages from a queue with containers, scaling with the backlog" → ECS service + SQS + Service Auto Scaling.
  • "Notify when a container stops unexpectedly" → ECS task state change event → EventBridge → SNS.

Common traps

  • "Service Auto Scaling also adds EC2 instances." It adds tasks; capacity providers (or ASG scaling) add instances.
  • "Fargate needs an Auto Scaling group." Fargate has no instances to scale.

Check yourself

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt