Sub topic 13
Reading Failures and Rollback States
This chapter assumes you can write a basic template, run aws cloudformation deploy, and know that a stack is the set of resources created from one template. You will learn how to find why an operation failed and what each rollback state means.
What happens when an operation fails
CloudFormation applies changes resource by resource. If one resource fails, it tries to return the stack to its last good state. The behavior depends on the operation.
- Create fails: by default the stack rolls back, and CloudFormation deletes every resource it created. The stack ends in
ROLLBACK_COMPLETE. A stack in this state cannot be updated. You can only delete it and create it again. - Update fails: CloudFormation restores the previous template and properties. The stack ends in
UPDATE_ROLLBACK_COMPLETE, and you can update it again. - Rollback itself fails: the stack ends in
UPDATE_ROLLBACK_FAILED(orROLLBACK_FAILEDafter a failed create). CloudFormation could not undo a change, often because someone modified or deleted a resource outside the stack.
Find the cause in stack events
The stack status only says that something failed. The reason is in the stack events. Events are listed newest first, so the first *_FAILED event in time order is usually the root cause. Later failures are often side effects of the rollback.
aws cloudformation describe-stack-events \
--stack-name demo \
--query "StackEvents[?contains(ResourceStatus, 'FAILED')].[Timestamp,LogicalResourceId,ResourceStatus,ResourceStatusReason]" \
--output tableThe ResourceStatusReason column holds the service's own error message. Typical causes are:
| Message theme | Likely cause |
|---|---|
| Bucket name already exists | S3 bucket names are global, so a fixed name collides |
| Access denied or not authorized | The caller or the service role lacks a permission |
| Resource limit exceeded | An account quota was reached |
| Resource creation cancelled | A different resource failed, so this one was stopped |
Read the original failure, not the "cancelled" entries that follow it.
Recovering from each state
For ROLLBACK_COMPLETE after a failed create, delete the stack, fix the template, and create it again.
For UPDATE_ROLLBACK_FAILED, fix the cause first, for example by recreating a resource that was deleted by hand. Then resume the rollback:
aws cloudformation continue-update-rollback --stack-name demoIf you cannot fix the resource, you can pass --resources-to-skip with the logical IDs to skip. The stack then no longer matches reality for those resources, so check for drift afterward.
Keeping failed resources for debugging
By default a failed create removes the evidence. To keep the resources that were created so you can inspect them, turn off rollback:
aws cloudformation create-stack \
--stack-name demo \
--template-body file://template.yaml \
--on-failure DO_NOTHINGThe stack stops in CREATE_FAILED. Delete it when you finish, because the partial resources still cost money.
Exercise
A new stack is in ROLLBACK_COMPLETE. You fix the template and run an update on the same stack, and it is rejected. Why, and what do you do?
Show answer
A stack in ROLLBACK_COMPLETE came from a failed create, so no resources remain and it cannot be updated. Delete the stack with aws cloudformation delete-stack, then create it again with the fixed template. Before that, read the first *_FAILED event in describe-stack-events to confirm the cause.