Learning center

CloudFormation ECS service did not stabilize: find the real failure

When a stack update waits on an AWS::ECS::Service and then fails, the stack event only says the service never settled. Find the first failed resource event and how long the service resource was in progress, then read the ECS deployment and its stopped tasks, which hold the cause.

What CloudFormation waits for#

CloudFormation does not mark an ECS service update complete when ECS accepts the new configuration. It polls the service until it reaches the state the template describes and only then reports the resource done. A service whose new tasks cannot launch or stay running holds the stack in UPDATE_IN_PROGRESS for hours. CloudFormation then fails the resource with a reason saying the service did not stabilize, and the stack starts to roll back. Service fails to stabilize in CloudFormation · ECS service stuck in an update

That reason names the resource and leaves the cause in ECS. Three nearby situations produce a failed ECS service in the stack events, and they need different fixes:

  • The new tasks start and then stop. The revision cannot start, its process exits, or it fails a health check. The new ECS deployment has stopped tasks with stop codes.
  • ECS never places the new tasks. No capacity or placement constraint fits them. The new deployment has no stopped tasks, and the service events name what failed.
  • The service is not the first failure. Another resource in the same update failed first and CloudFormation cancelled the rest, or ECS rejected the service update request outright. Either way the stack spent little or no time waiting on the service.

Read the stack events in time order#

Use a read-only profile and replace the uppercase placeholders. Start with the stack itself: its status, the role CloudFormation used and the latest operations.

aws cloudformation describe-stacks \
  --profile PROFILE --region REGION --stack-name STACK \
  --query 'Stacks[0].{status:StackStatus,reason:StackStatusReason,role:RoleARN,operations:LastOperations}' \
  --output json --no-cli-pager

describe-stack-events returns events newest first and covers the stack's whole history. This query keeps the stack's own status changes and every failed resource event, then reverses them into time order, so you can see where the update began and which resource failed first. Stack events · StackEvent fields

aws cloudformation describe-stack-events \
  --profile PROFILE --region REGION --stack-name STACK --max-items 300 \
  --query 'reverse(StackEvents[?ResourceType==`"AWS::CloudFormation::Stack"` || ends_with(ResourceStatus, `"FAILED"`)])[].[Timestamp,LogicalResourceId,ResourceType,ResourceStatus,ResourceStatusReason]' \
  --output json --no-cli-pager

Find the stack's last UPDATE_IN_PROGRESS event and read the first failure after it. Then pull the ECS service's own events for the same update to measure how long it was in progress:

aws cloudformation describe-stack-events \
  --profile PROFILE --region REGION --stack-name STACK --max-items 300 \
  --query 'reverse(StackEvents[?LogicalResourceId==`"SERVICE_LOGICAL_ID"`])[].[Timestamp,ResourceStatus,ResourceStatusReason]' \
  --output json --no-cli-pager

Newer CLI versions also offer describe-events, which takes an operation ID from LastOperations and a filter that returns only failed events for that operation. DescribeEvents

What the events showWhat it meansNext read
Another resource failed first; the service's later failure says its update was cancelledThe service did not fail on its ownThat first resource's ResourceStatusReason
The service went from UPDATE_IN_PROGRESS to UPDATE_FAILED within seconds or minutes, with a reason carrying an ECS errorECS rejected the request; CloudFormation never waited on tasksThe template property the error names
The service stayed UPDATE_IN_PROGRESS for hours, then failed with a reason about stabilizationECS accepted the update and the new deployment never reached steady stateThe ECS deployment, below

A resource can also be UPDATE_FAILED because the update was cancelled; the ContinueUpdateRollback API reference uses that case as an example. Do not chase a cancelled resource's reason. ContinueUpdateRollback

Then read the ECS deployment#

The service's physical ID comes from the stack resource. Pass it to describe-services with the cluster named in the template's Cluster property. Stack resources

aws cloudformation describe-stack-resources \
  --profile PROFILE --region REGION --stack-name STACK --logical-resource-id SERVICE_LOGICAL_ID \
  --query 'StackResources[0].{physical:PhysicalResourceId,status:ResourceStatus,reason:ResourceStatusReason}' \
  --output json --no-cli-pager

aws ecs describe-services \
  --profile PROFILE --region REGION --cluster CLUSTER --services SERVICE_PHYSICAL_ID \
  --query 'services[0].{deployments:deployments[].{id:id,status:status,definition:taskDefinition,desired:desiredCount,running:runningCount,pending:pendingCount,failed:failedTasks,rollout:rolloutState,reason:rolloutStateReason,created:createdAt},events:events[:10].[createdAt,message]}' \
  --output json --no-cli-pager

Pick the deployment whose definition is the new revision. Once a rollback has begun, a newer deployment for the previous revision can be PRIMARY. failedTasks counts consecutive tasks that could not launch, never reached RUNNING, or failed a health check and were stopped; it resets once the deployment has a running task. rolloutState is FAILED when the deployment circuit breaker is on and the service could not reach steady state, and a failed deployment launches no new tasks. Deployment fields · Deployment circuit breaker

list-service-deployments and describe-service-deployments give the same rollout a status such as ROLLBACK_SUCCESSFUL or ROLLBACK_FAILED and a statusReason, which says, for example, that the circuit breaker detected a failure. ServiceDeployment fields

Then list the stopped tasks and keep those whose startedBy matches that deployment's ID:

aws ecs list-tasks \
  --profile PROFILE --region REGION --cluster CLUSTER \
  --service-name SERVICE --desired-status STOPPED \
  --query 'taskArns' --output json --no-cli-pager

aws ecs describe-tasks \
  --profile PROFILE --region REGION --cluster CLUSTER --tasks TASK_ARN_1 TASK_ARN_2 \
  --query 'tasks[].{deployment:startedBy,definition:taskDefinitionArn,started:startedAt,stopped:stoppedAt,code:stopCode,reason:stoppedReason}' \
  --output json --no-cli-pager

If the new deployment has stopped tasks, group them by stop code and follow the stop code guide for each group; the fix belongs in the revision or its configuration. If it has none and running and pending stay at 0, read the service events for the placement constraint that failed; the fix is capacity, and the revision may be fine.

An illustrative stack update#

This fixture is fictional. Stack orders-prod updates OrdersService from task definition orders:57 to orders:58, and in the same update changes the IAM role OrdersTaskRole.

In the first outcome, the service is the first and only failure:

TimeRecordValue
09:58StackUPDATE_IN_PROGRESS
09:58OrdersServiceUPDATE_IN_PROGRESS
12:39ECS deployment ecs-svc/2211 (orders:58)Latest stopped tasks show TaskFailedToStart; the reason names a log group that does not exist
12:41OrdersServiceUPDATE_FAILED; the reason says the service did not stabilize
12:41StackUPDATE_ROLLBACK_IN_PROGRESS

Two hours and forty minutes in progress says ECS accepted the update and kept trying. The stopped tasks say the revision never started, so the fix is the log configuration in orders:58.

In the second outcome, OrdersTaskRole fails at 09:58 with a reason naming an IAM error, and OrdersService fails a few seconds later as cancelled. ECS has no orders:58 deployment, or one that CloudFormation is already rolling back. Reading ECS stopped tasks here would find nothing related; the IAM change is the failure.

Where this read misleads#

The wait outlasts the evidence. ECS keeps stopped tasks for at least an hour and returns only the 100 most recent service events, so after a multi-hour wait the first failures are gone. Without the circuit breaker the scheduler keeps launching tasks, and recent stopped tasks usually show the same failure. With the circuit breaker, a FAILED deployment stops launching, so its stopped tasks age out: read them early or keep ECS task state change events. Compare with the rest of the signature table. Stopped task errors · Service fields

The rollback waits too. CloudFormation returns the service to its previous configuration and waits for that to stabilize. If the previous revision cannot run either, because the capacity is gone or something it depends on was deleted, the stack can end in UPDATE_ROLLBACK_FAILED. That state has its own read: find the resource that can't go back. ECS service stuck in an update

Cancelling the update is a change, not a read. cancel-update-stack works only in UPDATE_IN_PROGRESS and rolls the stack back to its previous configuration, which starts the same wait on the previous revision. CancelUpdateStack

# Change: rolls the stack back to its previous configuration.
aws cloudformation cancel-update-stack \
  --profile PROFILE --region REGION --stack-name STACK --no-cli-pager

Whether the circuit breaker should own the service rollback, and why a deployment stays in progress, are separate questions. This guide only places the failure.

Questions#

Why does my CloudFormation stack stay in UPDATE_IN_PROGRESS on an ECS service?

CloudFormation waits until the ECS service reaches the state the template describes. If the new tasks keep failing or are never placed, the service never gets there, and the stack waits for hours before failing the resource.

How do I find the cause of an ECS service that did not stabilize?

Confirm from the stack events that the service was the first failure and stayed in progress for a long time. Then read the ECS deployment for the new revision and its stopped tasks, grouped by stop code; if there are no stopped tasks, read the service events for placement.

Should I cancel a stack update that is waiting on an ECS service?

Cancelling rolls the stack back to its previous configuration, and CloudFormation then waits for the previous revision to stabilize. Read the stopped tasks first so you know whether that revision can still run.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading