Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(custom-resource-handler): auto-delete-[objects|images] breaks on …
…cloudformation rollback (#29581) ### Issue # (if applicable) Closes #27199 ### Reason for this change Based on the way the custom resource is implemented, it is likely that unexpected behavior happens on Cloudformation rollback, i.e. the custom resource will prematurely delete the objects. Consider the following scenario: ``` UPDATE target resource (replacement, creates a new resource) UPDATE custom resource (old -> new, objects in old bucket are deleted) (...stuff happens...) ERROR, triggers a rollback UPDATE custom resource (new -> old) DELETE target resource (deletes the new resource, remembers the existing one) ``` We will have deleted objects in the bucket that has been rolled back to in this scenario, but the content is now gone. ### Description of changes Instead of deleting it right during update, we send back `PhysicalResourceId` in the event handler which if the id changes, it will let CFN to empty and delete the bucket at the end of the deployment. ### Description of how you validated changes New & updated tests. Also manually tested with deploying a template ``` const bucket = new s3.Bucket(this, 'Bucket', { removalPolicy: cdk.RemovalPolicy.DESTROY, bucketName: <a bucket name that's not used>, autoDeleteObjects: true, }); // Intentionally failure since `mybucket-1` exists const bucket2 = new s3.Bucket(this, 'Bucket2', { removalPolicy: cdk.RemovalPolicy.DESTROY, bucketName: <a bucket name that's not used>, }); bucket2.node.addDependency(bucket); ``` Once the deployment is successful, add some random content to the bucket, then update the code so that the first bucket's bucketName is updated to another valid name. Update the second bucket's bucketName to be an existing bucket name, which will trigger a deployment failure hence roll back. After the change, the content will stay there if a deployment failure happens. The content & bucket will be deleted if deployment is successful. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information