A CloudFormation template creates an EC2 instance (with user data to install an app) and an S3 bucket used by the app to serve static pages. All resources must be removed when the stack is deleted, but stack deletion fails because the S3 bucket is not removed. What is the most efficient way to ensure all stack resources are deleted without errors?
Choose an answer
Tap an option to check your answer.
Correct answer: Add a custom resource backed by an AWS Lambda function (with an IAM role) that depends on the S3 bucket. Have the Lambda handler delete all objects from the bucket when RequestType is Delete..
Why this is the answer
S3 buckets must be empty before they can be deleted. CloudFormation stack deletion fails if a bucket contains objects. The most efficient solution is to use a custom resource backed by a Lambda function. This Lambda function can be triggered during stack deletion (RequestType: Delete) to empty the S3 bucket before CloudFormation attempts to delete it. This ensures a clean and automated stack deletion. Adding DeletionPolicy: Delete to the S3 bucket is insufficient because it only allows CloudFormation to delete the bucket if it's empty; it doesn't empty the bucket. Manually emptying and deleting the bucket is not an automated or efficient solution for repeated deployments. Using AWS OpsWorks Stacks is an overly complex solution for this specific problem and doesn't directly address the S3 bucket emptying requirement during CloudFormation stack deletion.
Pass your exam — without the endless answer hunt
Get every verified question and explanation for this exam in one place, and save hours of prep. 1,000+ certifications · 20+ languages · free to start.
Pass your exam faster → No card needed