Which solution meets these requirements?
Implement a Lambda function that deletes all files from a given S3 bucket. Integrate this Lambda function as a custom resource into the CloudFormation stack. Ensure that the custom resource has a DependsOn attribute that points to the S3 bucket’s resource.
Modify the CloudFormation template to provision an Amazon Elastic File System (Amazon EFS) file system to store the temporary files there instead of in Amazon S3. Configure the Lambda functions to run in the same VPC as the file system. Mount the file system to the EC2 instances and Lambda functions.
Modify the CloudF ormation stack to create an S3 Lifecycle rule that expires all objects 45 minutes after creation. Add a DependsOn attribute that points to the S3 bucket’s resource.
Modify the CloudFormation stack to attach a DeletionPolicy attribute with a value of Delete to the S3 bucket.
Explanations:
Implementing a Lambda function as a custom resource to delete all files from the S3 bucket before CloudFormation deletes the stack is the correct solution. CloudFormation might fail to delete an S3 bucket if it contains objects, and a custom Lambda function that deletes these objects ensures that the bucket can be deleted successfully. TheDependsOnattribute ensures the bucket is cleared before the deletion of other resources.
Modifying the application to use Amazon EFS instead of S3 for temporary file storage introduces a significant architectural change. This is not necessary to resolve the issue with S3 bucket deletion and would be a more complex solution that is not aligned with the requirement to avoid major changes to the architecture.
Adding an S3 Lifecycle rule to expire objects 45 minutes after creation doesn’t address the issue of S3 bucket deletion during stack termination. The lifecycle rule can remove objects over time, but CloudFormation may still fail to delete the bucket if objects remain when the stack is deleted. This doesn’t guarantee bucket deletion.
TheDeletionPolicy: Deleteattribute for an S3 bucket in CloudFormation only ensures that the bucket is deleted when the stack is deleted. However, if the bucket contains objects, the delete operation will fail. This does not resolve the issue where the S3 bucket cannot be deleted due to existing objects, and additional steps (like deleting objects before stack deletion) are required.