AWS Certified Solutions Architect – Professional (SAP-C02) — Question 320

A financial services company runs a complex, multi-tier application on Amazon EC2 instances and AWS Lambda functions. The application stores temporary data in Amazon S3. The S3 objects are valid for only 45 minutes and are deleted after 24 hours.

The company deploys each version of the application by launching an AWS CloudFormation stack. The stack creates all resources that are required to run the application. When the company deploys and validates a new application version, the company deletes the CloudFormation stack of the old version.

The company recently tried to delete the CloudFormation stack of an old application version, but the operation failed. An analysis shows that CloudFormation failed to delete an existing S3 bucket. A solutions architect needs to resolve this issue without making major changes to the application's architecture.

Which solution meets these requirements?

Answer options

Correct answer: A

Explanation

AWS CloudFormation cannot delete an Amazon S3 bucket if it still contains objects. By creating a custom resource powered by an AWS Lambda function, you can programmatically empty the S3 bucket before CloudFormation attempts to delete the bucket resource itself. The DependsOn attribute ensures the cleanup Lambda runs and completes before the bucket deletion is initiated, while other options either require significant architectural changes or do not guarantee an empty bucket at the exact moment of stack deletion.