Home / AWS

AWSTroubleshootingAdvanced

ECS service auto scaling not scaling out or in: scalable targets, suspended activities, alarms and desired count resets

CPU is pegged but the ECS service stays at the same task count, or it never scales back in. Read Application Auto Scaling activities, not-scaled reasons and alarms to find which layer is blocking.

Deepak Bhardwaj6 min read

Applies toAmazon ECS services on Fargate and EC2, Application Auto Scaling (target tracking and step scaling), AWS CLI v2

Symptom

  • Service CPU or request count is well above target, but desiredCount doesn't change.
  • Or the service scaled out during a peak and stays at maximum for hours.
  • Or it scales out, then a few minutes later the task count goes back to the old number.

ECS doesn't scale services itself. Application Auto Scaling owns the scaling, CloudWatch alarms trigger it, and ECS only receives a new desired count. Check each layer in order.

CLUSTER=prod
SERVICE=orders-api
RID=service/$CLUSTER/$SERVICE

1. Is there a scalable target, and is it suspended?

aws application-autoscaling describe-scalable-targets \
  --service-namespace ecs --resource-ids $RID \
  --query 'ScalableTargets[].{Min:MinCapacity,Max:MaxCapacity,Suspended:SuspendedState,Role:RoleARN}'
Result Meaning
Empty list No auto scaling is registered. Policies set up in the console during a service recreate were lost, or IaC never created them
Min = Max The service can't move. Someone pinned it during an incident
DynamicScalingOutSuspended: true Scale-out from alarms is switched off
DynamicScalingInSuspended: true Scale-in is switched off. Tasks stay at peak
ScheduledScalingSuspended: true Scheduled actions are skipped

Suspension usually comes from a past incident or a deployment script that never cleaned up. Resume:

aws application-autoscaling register-scalable-target \
  --service-namespace ecs --resource-id $RID \
  --scalable-dimension ecs:service:DesiredCount \
  --suspended-state '{"DynamicScalingInSuspended":false,"DynamicScalingOutSuspended":false,"ScheduledScalingSuspended":false}'

2. What did Application Auto Scaling decide?

aws application-autoscaling describe-scaling-activities \
  --service-namespace ecs --resource-id $RID \
  --scalable-dimension ecs:service:DesiredCount \
  --include-not-scaled-activities \
  --max-items 20 \
  --query 'ScalingActivities[].{Time:StartTime,Status:StatusCode,Cause:Cause,Msg:StatusMessage,NotScaled:NotScaledReasons[].Code}'

--include-not-scaled-activities is the key flag. Without it you only see actions that happened, not the decisions not to act.

Not-scaled reason or status Cause Fix
AlreadyAtMaxCapacity Alarm fired but MaxCapacity reached Raise max, after checking cluster or Fargate capacity and quotas
AlreadyAtMinCapacity Scale-in wanted but already at min Expected, or lower min
TargetServicePutResourceAsUnscalable ECS reported the service can't be scaled right now Check service state and recent deployments
Failed with an access message Service-linked role missing or blocked by an SCP See step 5
No activities at all Alarms never went to ALARM Step 3

Only the first not-scaled activity for a given reason is recorded, and history covers about six weeks. A single entry can stand for many skipped decisions.

3. Are the alarms firing?

Target tracking creates its own alarms named TargetTracking-service/<cluster>/<service>-AlarmHigh-... and -AlarmLow-....

aws application-autoscaling describe-scaling-policies \
  --service-namespace ecs --resource-id $RID \
  --query 'ScalingPolicies[].{Name:PolicyName,Type:PolicyType,Config:TargetTrackingScalingPolicyConfiguration,Alarms:Alarms[].AlarmName}'

aws cloudwatch describe-alarms \
  --alarm-name-prefix "TargetTracking-$RID" \
  --query 'MetricAlarms[].{Name:AlarmName,State:StateValue,Reason:StateReason,Actions:ActionsEnabled}'

Warning

Never edit or delete the TargetTracking-* alarms by hand. Application Auto Scaling manages them. Changes are overwritten or the policy breaks. To change behaviour, update the policy with put-scaling-policy.

Common findings:

  • Alarm in INSUFFICIENT_DATA: the metric isn't published. For ALBRequestCountPerTarget, the ResourceLabel must exactly match app/<alb-name>/<alb-id>/targetgroup/<tg-name>/<tg-id>. A recreated target group gets a new ID and the old label points at nothing.
  • Alarm OK while dashboards look hot: you're watching maximum or per-container CPU. The ECSServiceAverageCPUUtilization metric is the service average against the task-level CPU reservation. One hot task among ten won't move the average much.
  • Step scaling alarms with ActionsEnabled: false: someone disabled actions. Re-enable with aws cloudwatch enable-alarm-actions --alarm-names <name>.

Check the metric the policy uses:

aws cloudwatch get-metric-statistics \
  --namespace AWS/ECS --metric-name CPUUtilization \
  --dimensions Name=ClusterName,Value=$CLUSTER Name=ServiceName,Value=$SERVICE \
  --start-time $(date -u -d '-1 hour' +%FT%TZ) --end-time $(date -u +%FT%TZ) \
  --period 60 --statistics Average Maximum

4. Scaled, then reset: who changed desired count?

If activities show a successful scale-out and the count later drops back, something else set desiredCount. Check CloudTrail for UpdateService calls that weren't from Application Auto Scaling:

aws cloudtrail lookup-events \
  --lookup-attributes AttributeKey=EventName,AttributeValue=UpdateService \
  --max-results 20 \
  --query 'Events[].{Time:EventTime,User:Username,Src:CloudTrailEvent}' --output json \
  | grep -oE '"(eventTime|userAgent|desiredCount|service)":[^,]*'

Usual sources:

Source Fix
Terraform aws_ecs_service with desired_count Add lifecycle { ignore_changes = [desired_count] }
CloudFormation DesiredCount on AWS::ECS::Service Leave DesiredCount unset on updates, or keep it at the scaling minimum. Check current CloudFormation docs for your template pattern
Deploy script calling update-service --desired-count Remove the flag. Deploy with --force-new-deployment or a new task definition only
Scheduled action with a fixed min/max Review with describe-scheduled-actions
aws application-autoscaling describe-scheduled-actions \
  --service-namespace ecs --resource-id $RID \
  --query 'ScheduledActions[].{Name:ScheduledActionName,Schedule:Schedule,Min:ScalableTargetAction.MinCapacity,Max:ScalableTargetAction.MaxCapacity}'

5. Scale-in never happens

  • Deployment in progress: Application Auto Scaling pauses scale-in while an ECS deployment runs. Scale-out continues. A deployment stuck IN_PROGRESS blocks scale-in for as long as it runs. Check with aws ecs describe-services --cluster $CLUSTER --services $SERVICE --query 'services[].deployments[].{Status:status,Rollout:rolloutState,Running:runningCount}'.
  • DisableScaleIn: true in the target tracking configuration from step 3.
  • Long ScaleInCooldown: values in thousands of seconds make the service look stuck.
  • Low-alarm never reached: target tracking scales in more cautiously than it scales out. It needs a sustained run of datapoints below target. A target too close to idle usage, such as 20% CPU, may never be crossed.

6. Permissions

Application Auto Scaling uses the service-linked role AWSServiceRoleForApplicationAutoScaling_ECSService.

aws iam get-role --role-name AWSServiceRoleForApplicationAutoScaling_ECSService \
  --query 'Role.Arn'

If it's missing, registering a scalable target normally creates it. Creation fails if the caller lacks iam:CreateServiceLinkedRole. An SCP that denies ecs:UpdateService or ecs:DescribeServices without exempting this role blocks every scaling action. The failed activity's StatusMessage shows the access error.

7. Desired count went up but tasks don't start

At this point auto scaling has worked and the problem is placement:

aws ecs describe-services --cluster $CLUSTER --services $SERVICE \
  --query 'services[].events[:10].message'

unable to place a task messages point to EC2 cluster capacity or a capacity provider problem. On Fargate, check the On-Demand vCPU quota in Service Quotas and subnet free IPs. Check the current quota values rather than assuming defaults.

A sane target tracking policy

cat > tt-cpu.json <<'EOF'
{
  "TargetValue": 60.0,
  "PredefinedMetricSpecification": {
    "PredefinedMetricType": "ECSServiceAverageCPUUtilization"
  },
  "ScaleOutCooldown": 60,
  "ScaleInCooldown": 300,
  "DisableScaleIn": false
}
EOF

aws application-autoscaling put-scaling-policy \
  --service-namespace ecs --resource-id $RID \
  --scalable-dimension ecs:service:DesiredCount \
  --policy-name cpu-60 --policy-type TargetTrackingScaling \
  --target-tracking-scaling-policy-configuration file://tt-cpu.json

Tip

For request-driven web services, add a second target tracking policy on ALBRequestCountPerTarget. With several policies, Application Auto Scaling scales out if any policy asks for it. It scales in only when all of them agree.

Prevent it next time

  • Define scalable targets and policies in the same IaC as the service, and ignore desired_count drift.
  • Alarm on the service sitting at MaxCapacity: compare RunningTaskCount from Container Insights with your max, or alert on AlreadyAtMaxCapacity activities.
  • After any incident where someone suspends scaling or pins min = max, add an explicit "resume scaling" step to the incident close-out.
  • Keep deployments short with the circuit breaker enabled, so a stuck rollout doesn't block scale-in for hours.