Q22 — AWS DOP-C02 Ch.3
Question 22 of 100 | ← Chapter 3
A company developed a static website hosted on an Amazon S3 bucket. The website serves cloud imagery from astronomical observatories. A CloudFormation template defines the S3 bucket and a custom resource that copies content to the bucket. The company decided to move the website to a new location and attempted to delete and recreate the existing stack. However, CloudFormation reports that the stack cannot be cleanly deleted. What is the most likely cause, and how can this issue be mitigated for both this and future versions of the stack?
- A. Deletion fails because the S3 bucket has an active website configuration. Modify the CloudFormation template to remove the WebsiteConfiguration property from the S3 bucket resource.
- B. Deletion fails because the S3 bucket is not empty. Modify the custom resource’s AWS Lambda function code to recursively empty the bucket when a Delete request type is received. ✓
- C. Deletion fails because the custom resource does not define a deletion policy. Add a DeletionPolicy attribute with value Delete to the custom resource definition.
- D. Deletion fails because the S3 bucket is not empty. Modify the S3 bucket resource in the CloudFormation template to add a DeletionPolicy attribute with an empty value.
Correct Answer: B. Deletion fails because the S3 bucket is not empty. Modify the custom resource’s AWS Lambda function code to recursively empty the bucket when a Delete request type is received.
Explanation
CloudFormation cannot delete an S3 bucket containing objects unless explicitly instructed to do so. AWS documentation states that deleting a non-empty bucket requires first emptying its contents. Option B correctly implements the recommended practice: using a Lambda-backed custom resource to recursively delete bucket contents during stack deletion. Option D is invalid—DeletionPolicy accepts only Retain, Snapshot, or Delete, and Delete still requires the bucket to be empty. Options A and C do not resolve the root cause (non-empty bucket).