Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

‼️ s3: toggling off auto_delete_objects for Bucket empties the bucket #16603

Closed
rittneje opened this issue Sep 22, 2021 · 10 comments · Fixed by #16756
Closed

‼️ s3: toggling off auto_delete_objects for Bucket empties the bucket #16603

rittneje opened this issue Sep 22, 2021 · 10 comments · Fixed by #16756
Assignees
Labels
@aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. effort/small Small work item – less than a day of effort p0

Comments

@rittneje
Copy link

rittneje commented Sep 22, 2021

If a stack is deployed with an S3 bucket with auto_delete_objects=True, and then re-deployed with auto_delete_objects=False, all the objects in the bucket will be deleted.

Reproduction Steps

  1. Deploy a stack with an S3 bucket created with auto_delete_objects=True.
  2. Upload files into the bucket.
  3. Deploy the stack again with auto_delete_objects=False.
  4. Observe that the bucket is now empty.

What did you expect to happen?

The bucket should not have been emptied.

What actually happened?

Since the custom resource was deleted, the bucket was emptied.

Environment

  • CDK CLI Version : 1.118.0
  • Framework Version:
  • Node.js Version: 14.17.5
  • OS : debian bullseye (running inside a container)
  • Language (Version): Python 3.9.6

This is 🐛 Bug Report

@rittneje rittneje added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 22, 2021
@github-actions github-actions bot added the @aws-cdk/aws-s3 Related to Amazon S3 label Sep 22, 2021
@ryparker
Copy link
Contributor

ryparker commented Sep 29, 2021

Hey @rittneje thanks for letting us know about this.

I confirmed that the S3 is emptied when toggling autoDeleteObjects from true to false. Even with removalPolicy set to RemovalPolicy.RETAIN.

Repro code

import { App, Stack, RemovalPolicy } from '@aws-cdk/core';
import { Bucket, BlockPublicAccess } from '@aws-cdk/aws-s3'

const app = new App();
const stack = new Stack(app, 'issue-16603');

const bucket = new Bucket(stack, 'Issue16603TestBucket', {
  bucketName: 'issue-16603-test-bucket',
  blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
  removalPolicy: RemovalPolicy.DESTROY, // change to `.RETAIN` on 2nd deploy
  autoDeleteObjects: true // change to `false` on 2nd deploy
});

Labeling this as p1 which means it has been prioritized as important, although please keep in mind that we do have a large number of issues at the moment. It may be some time before we are able to solve this particular issue. We use +1s to help us prioritize our work, and as always we are happy to take contributions if anyone is interested to pick this up and submit a PR.

@ryparker ryparker added p1 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Sep 29, 2021
@kellertk kellertk added p0 and removed p1 labels Sep 30, 2021
@kellertk
Copy link
Contributor

Upgrading to p0 based on the potential to lose data without warning.

@rix0rrr rix0rrr pinned this issue Oct 1, 2021
@rix0rrr rix0rrr changed the title aws-s3: toggling off auto_delete_objects for Bucket empties the bucket !!WARNING!!s3: toggling off auto_delete_objects for Bucket empties the bucket Oct 1, 2021
@rix0rrr rix0rrr changed the title !!WARNING!!s3: toggling off auto_delete_objects for Bucket empties the bucket !!WARNING!! s3: toggling off auto_delete_objects for Bucket empties the bucket Oct 1, 2021
rix0rrr added a commit that referenced this issue Oct 1, 2021
This was caused by the Custom Resource--which had previously been
deployed when `autoDeleteObjects: true`--being removed when
`autoDeleteObjects` is flipped off again. The custom resource would
indiscriminately empty the bucket as it was being deleted.

Fix by having the custom resource inspect the ongoing CloudFormation
deployment: if the bucket would not be deleted as part of the ongoing
deployment, also do not empty it.

Fixes #16603.
@ryparker ryparker assigned rix0rrr and unassigned otaviomacedo Oct 1, 2021
@rix0rrr rix0rrr removed their assignment Oct 4, 2021
@mergify mergify bot closed this as completed in #16756 Oct 4, 2021
mergify bot pushed a commit that referenced this issue Oct 4, 2021
…16756)

This was caused by the Custom Resource--which had previously been
deployed when `autoDeleteObjects: true`--being removed when
`autoDeleteObjects` is flipped off again. The custom resource would
indiscriminately empty the bucket as it was being deleted.

Fix by tagging the bucket to confirm that it needs to be emptied. If
any deployment removes the CR but keeps the bucket, the ordering of
CloudFormation updates will make sure that the untagging happens before
the CR gets activated, thereby saving the bucket contents.

Fixes #16603.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Oct 4, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@ryparker
Copy link
Contributor

ryparker commented Oct 5, 2021

Fix released in v1.126.0

If you have deployed a bucket with autoDeleteObjects: true, switching this to false in a CDK version before 1.126.0 will lead to all objects in the bucket being deleted. Be sure to deploy your CDK application using version 1.126.0 or later before switching this value to false.

@rittneje
Copy link
Author

rittneje commented Oct 6, 2021

@ryparker To be clear, I think you have to actually deploy the stack in question with 1.126.0+ at least once before switching the value to false. If the deployment that switches it is also the first to use 1.126.0+, then I believe the older version of the custom resource would still be invoked.

@ryparker
Copy link
Contributor

ryparker commented Oct 6, 2021

@ryparker To be clear, I think you have to actually deploy the stack in question with 1.126.0+ at least once before switching the value to false. If the deployment that switches it is also the first to use 1.126.0+, then I believe the older version of the custom resource would still be invoked.

Good clarification. I'll update that in our docs as well.

@pfried
Copy link

pfried commented Oct 8, 2021

Thanks for the mailing!

@eladb eladb changed the title !!WARNING!! s3: toggling off auto_delete_objects for Bucket empties the bucket ‼️ s3: toggling off auto_delete_objects for Bucket empties the bucket Oct 8, 2021
@piotrekwitkowski
Copy link

@ryparker what about CDK v2? Do you know what is the min version I can safely redeploy with?

njlynch pushed a commit that referenced this issue Oct 11, 2021
…16756)

This was caused by the Custom Resource--which had previously been
deployed when `autoDeleteObjects: true`--being removed when
`autoDeleteObjects` is flipped off again. The custom resource would
indiscriminately empty the bucket as it was being deleted.

Fix by tagging the bucket to confirm that it needs to be emptied. If
any deployment removes the CR but keeps the bucket, the ordering of
CloudFormation updates will make sure that the untagging happens before
the CR gets activated, thereby saving the bucket contents.

Fixes #16603.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@benm5678
Copy link

Can anyone say if we need to update all package references, or if it's enough to just have the latest CDK cli?
We use some of the aws-solution-constructs & max version there is still 1.125 (and all CDK pkg versions need to match to avoid errors)

@otaviomacedo
Copy link
Contributor

@benm5678 You need to update all the references.

@otaviomacedo otaviomacedo unpinned this issue Oct 25, 2021
@otaviomacedo otaviomacedo pinned this issue Oct 25, 2021
@rix0rrr rix0rrr unpinned this issue Nov 29, 2021
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
…ws#16756)

This was caused by the Custom Resource--which had previously been
deployed when `autoDeleteObjects: true`--being removed when
`autoDeleteObjects` is flipped off again. The custom resource would
indiscriminately empty the bucket as it was being deleted.

Fix by tagging the bucket to confirm that it needs to be emptied. If
any deployment removes the CR but keeps the bucket, the ordering of
CloudFormation updates will make sure that the untagging happens before
the CR gets activated, thereby saving the bucket contents.

Fixes aws#16603.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. effort/small Small work item – less than a day of effort p0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants