AWS Certified Developer – Associate
Why did my CloudFormation stack fail? DVA-C03 troubleshooting
The CloudFormation User Guide says to open the stack's Events tab, find the failure event and read its status reason. For a failed creation, Detect root cause labels the event that is the likely root cause. To keep the resources that were created, choose Preserve successfully provisioned resources, or --disable-rollback in the AWS CLI, then retry.
Where is the reason for the failure?
CloudFormation records each stack operation as a list of stack events. Find the failure event and read its status reason. It can hold an error message from CloudFormation or from the service that owns the resource. Events carry operation IDs, and you can filter by operation and by failures.
When a stack creation fails, choose Detect root cause on the Events tab. CloudFormation analyzes the failure and adds a Likely root cause label to the status of the event that probably caused it. If the status reason is unclear and your permissions allow, the status can link to related CloudTrail events. Nested stacks do not support Detect root cause.
For EC2 instances, the cloud-init and cfn logs in /var/log/ record what happened during setup, for example /var/log/cfn-init.log. You can read them by logging in to the instance only if rollback on failure is disabled. Otherwise CloudFormation deletes the instance after the stack fails to create.
What do the stack failure options do?
By default a failed operation rolls back. The Preserve successfully provisioned resources option changes that. On a create, CloudFormation keeps the resources that were created and leaves the failed ones in a failed state, with the stack in CREATE_FAILED. On an update, failed resources roll back to their last known stable state and show UPDATE_FAILED.
From the AWS CLI, the same option is --disable-rollback on create-stack or update-stack. On create-stack you can also use --on-failure DO_NOTHING.
After you fix the cause, choose Retry if the template does not need to change, for example after correcting an IAM permission. Choose Update if you changed the template. Choose Roll back, or run rollback-stack, to return to the last known stable state. Provisioning continues from the point of failure instead of starting over.
Why does a delete fail, and how do you hear about failures?
Some resources must be empty before they can be deleted. CloudFormation cannot delete an S3 bucket until all of its objects are deleted. You also need IAM permissions for the underlying services.
When a stack is in DELETE_FAILED, either remove the obstacle and delete again, or rerun the deletion with the RetainResources parameter naming the resource that cannot be deleted. CloudFormation then deletes the stack and leaves that resource in place.
CloudFormation sends events to the default EventBridge event bus whenever a create, update or delete operation is performed on a stack, including Stack Status Change events. A rule on that bus can match a status and send it to a target. CloudFormation needs no extra permissions to deliver these events.
Where the exam catches people
- Believing you must read every event to find the cause of a failed create: Detect root cause on the Events tab labels the likely root cause event.
- Believing termination protection or a stack policy keeps created resources after a failure: the setting for that is Preserve successfully provisioned resources, which is
--disable-rollbackin the CLI. - Believing CloudFormation empties an S3 bucket before deleting it: the bucket must be empty, or the delete must be rerun with
RetainResourcesfor the bucket. - Believing
/var/log/cfn-init.logis still readable after a default rollback: the instance is deleted unless rollback on failure is disabled. - Believing CloudFormation events need a custom event bus: they go to the default EventBridge event bus in the account.