Desired Count

The desired count is how many task copies a service tries to keep running.

A service's desired count is the number of tasks it should keep RUNNING. If a task stops, the service launches a replacement to restore the count.

Changing it

Scale manually by updating the desired count, or let auto scaling change it for you based on load.

Example

Example · bash
# Scale to 5 tasks
aws ecs update-service \
  --cluster prod --service web \
  --desired-count 5

When to use it

  • An SRE sets the desired count to 3 across three AZs so the service survives an AZ outage with two tasks still running.
  • An on-call engineer immediately sets desired count to 0 to kill all tasks of a misbehaving service without deleting the service definition.
  • A load tester temporarily bumps desired count from 2 to 20 before a stress test and scales back down after to confirm auto-scaling thresholds.

More examples

Set Desired Count on Service Creation

Creates a service with desired count 3 spread across three subnets in different AZs for basic fault tolerance.

Example · bash
aws ecs create-service \
  --cluster prod \
  --service-name api \
  --task-definition api:5 \
  --desired-count 3 \
  --launch-type FARGATE \
  --network-configuration \
    'awsvpcConfiguration={subnets=[subnet-a,subnet-b,subnet-c],securityGroups=[sg-api],assignPublicIp=DISABLED}'

Update Desired Count on Running Service

Instantly scales the service to 10 tasks; ECS launches new tasks to reach the target while keeping existing healthy tasks running.

Example · bash
aws ecs update-service \
  --cluster prod \
  --service api \
  --desired-count 10

Scale Service to Zero (Emergency Stop)

Sets desired count to zero and confirms that running count is decreasing to zero, effectively emergency-stopping the service.

Example · bash
aws ecs update-service \
  --cluster prod \
  --service api \
  --desired-count 0

# Verify tasks are draining
aws ecs describe-services \
  --cluster prod --services api \
  --query 'services[0].{desired:desiredCount,running:runningCount}'

Discussion

  • Be the first to comment on this lesson.