Certopolis

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-rollback in the CLI.
  • Believing CloudFormation empties an S3 bucket before deleting it: the bucket must be empty, or the delete must be rerun with RetainResources for the bucket.
  • Believing /var/log/cfn-init.log is 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.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. An AWS CloudFormation stack failed to create. Its event history holds dozens of failure and rollback events, and most of them report only that resource creation was cancelled. A developer must find which event caused the stack failure without reading through every event. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Troubleshooting and Optimization building.

    Not that one: C is right. In the game your Troubleshooting and Optimization building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Opening that status can also link to related AWS CloudTrail events when the status reason alone is unclear and permissions allow.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/determine-root-cause-for-stack-failures.html

  2. A developer creates an AWS CloudFormation stack of 40 resources from the AWS CLI. An Amazon EC2 instance near the end of the template keeps failing to create, and after each failure all of the created resources are deleted and take 30 minutes to create again. The developer wants the other resources to stay in place while the instance is investigated, and then wants the stack operation to continue from the instance. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Troubleshooting and Optimization building.

    Not that one: B is right. In the game your Troubleshooting and Optimization building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    When the instance fails, CloudFormation stops at that failure, keeps the resources that were created, and leaves the stack in CREATE_FAILED. After the cause is fixed, a retry or an update provisions the failed instance and continues from that point instead of starting over.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stack-failure-options.html

  3. A developer deletes an AWS CloudFormation stack that contains an Amazon S3 bucket, an AWS Lambda function, and an IAM role. The stack enters the DELETE_FAILED state because the bucket still contains objects. The developer must get the stack deletion to complete. Which solutions will meet these requirements? (Select TWO.)

    Right. In the game that’s Renown for your Troubleshooting and Optimization building.

    Not that one: A,E is right. In the game your Troubleshooting and Optimization building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Alternatively, rerunning the deletion with RetainResources naming the bucket tells CloudFormation to delete the stack but leave that resource behind; the developer can then delete the retained bucket separately through Amazon S3, or keep it if its objects are still needed.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html

Ask for something