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

fix(s3): setting autoDeleteObjects to false empties the bucket #16756

Merged
merged 12 commits into from
Oct 4, 2021

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented 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 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

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.
@rix0rrr rix0rrr requested a review from a team October 1, 2021 16:00
@rix0rrr rix0rrr self-assigned this Oct 1, 2021
@gitpod-io
Copy link

gitpod-io bot commented Oct 1, 2021

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Oct 1, 2021
Copy link
Contributor

@ryparker ryparker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix 🚀

@ryparker ryparker added the p0 label Oct 1, 2021
@ryparker
Copy link
Contributor

ryparker commented Oct 1, 2021

Looks like CI found an integration test that needs an update:
yarn integ integ.bucket-auto-delete-objects.js

rix0rrr and others added 2 commits October 1, 2021 19:06
@rittneje
Copy link

rittneje commented Oct 1, 2021

@rix0rrr @ryparker FWIW, as a workaround, we were investigating using CDK metadata on the S3 bucket as a means of telling the lambda whether it should actually clear the bucket or not. (The metadata would be set when auto_delete_objects is true, and unset otherwise.) I'm not sure how airtight that is though.

Comment on lines 117 to 120
const destinationTemplateResponse = await cfn.getTemplate({ StackName: stackId, TemplateStage: 'Processed' }).promise();
let template;
try {
template = yaml.parse(destinationTemplateResponse.TemplateBody ?? '{}', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you describe the changeset here instead of analyzing the template then you don't need the yaml dependency (and the script to include it in the handler). Better?

Copy link
Contributor Author

@rix0rrr rix0rrr Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. It artificially limits the template's use to cases where we deploy using change sets. Granted, the CDK always does that, but I hate unnecessarily limiting ourselves in that way.

tgt=lib/auto-delete-objects-handler/node_modules/yaml
if [[ ! -d $tgt ]]; then
mkdir -p $tgt
cp -R node_modules/yaml/ $tgt/
Copy link
Contributor

@jogold jogold Oct 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if yaml gets updated and comes with dependencies? (tests will continue to pass)

Copy link
Contributor

@iliapolo iliapolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rix0rrr Have you considered coupling the custom resource with the bucket, instead of the autoDeleteObjects property?

I.e the CR will always exist, and the lambda will either no-op if autoDeleteObjects is false (or undefined), or will delete otherwise.

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Oct 4, 2021

@rix0rrr Have you considered coupling the custom resource with the bucket, instead of the autoDeleteObjects property?

We have considered it. It means that every single one of the 1243891 templates out there that contains an S3 bucket will automatically gain 3 more resources:

  • Role
  • Lambda
  • CR

And so be "polluted", not to mention the diff we'd be introducing to all customers everywhere. I don't think it would be looked upon graciously

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Oct 4, 2021

@rix0rrr @ryparker FWIW, as a workaround, we were investigating using CDK metadata on the S3 bucket as a means of telling the lambda whether it should actually clear the bucket or not. (The metadata would be set when auto_delete_objects is true, and unset otherwise.) I'm not sure how airtight that is though.

You know what -- I rejected that initially because of assumptions I made on the CFN lifecycle. Specifically: I thought the following use case would be broken:

  • Bucket already exists with data
  • New deployment activates autoDeleteObjects: true, but gets rolled back.
  • In the process of applying autoDeleteObjects, we would set a tag on the bucket:

My assumption was that the rollback would happen in reverse topological order, and so the CR deletion would happen before the untagging:

UPDATE_IN_PROGRESS UPDATE_ROLLBACK_IN_PROGRESS
Tag bucket -> create custom resource -> (ROLLBACK) -> delete custom resource -> untag

However, after documenting the lifecycle, I think the order would actually be:

UPDATE_IN_PROGRESS UPDATE_ROLLBACK_IN_PROGRESS UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS
Tag bucket -> create custom resource -> (ROLLBACK) -> untag -> delete custom resource

In which case the tag-based approach is actually completely valid, and requires a smaller code change.

I think I will pivot to that. Thanks for reminding me!

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Oct 4, 2021

monocdk: FATAL ERROR: NewSpace::Rebalance Allocation failed - JavaScript heap out of memory

@mergify
Copy link
Contributor

mergify bot commented Oct 4, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@@ -0,0 +1,7 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file?

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: cfa9a41
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 21836f2 into master Oct 4, 2021
@mergify mergify bot deleted the huijbers/fix-s3-autodelete branch October 4, 2021 15:26
@mergify
Copy link
Contributor

mergify bot commented Oct 4, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

xykkong added a commit to xykkong/aws-cdk that referenced this pull request Oct 6, 2021
* '15588' of https://github.com/xykkong/aws-cdk: (47 commits)
  chore: rollback `GenericSSMParameterImage` deprecation (backport aws#16798) (aws#16800)
  chore(deps): bump actions/setup-node from 2.4.0 to 2.4.1 (aws#16778)
  Update CHANGELOG.md
  chore(release): 1.126.0
  feat(assertions): matcher support for `templateMatches()` API (aws#16789)
  feat(stepfunctions-tasks): add step concurrency level to EmrCreateCluster (aws#15242)
  docs(s3): correct heading levels Object Ownership / Bucket deletion (aws#16790)
  chore(individual-pkg-gen): fix bug in setting alpha package visibility (aws#16787)
  fix(s3): setting `autoDeleteObjects` to `false` empties the bucket (aws#16756)
  fix(iam): `User.fromUserArn` does not work for ARNs that include a path (aws#16269)
  fix(cli): progress bar overshoots count by 1 for stack updates (aws#16168)
  fix(config): add SourceAccount condition to Lambda permission (aws#16617)
  docs(events): add a note about not using `EventPattern` with `CfnRule` (aws#16715)
  docs(core): fix reference to nonexistant enum value (aws#16716)
  chore(s3-deployments): update python version on BucketDeployment handler (aws#16771)
  chore: set response-requested length to 2 and closing-soon to 5 (aws#16763)
  fix(revert): "fix: CDK does not honor NO_PROXY settings (aws#16751)" (aws#16761)
  docs(GitHub issue templates): Upgrade to GitHub Issues v2 (aws#16592)
  chore: reset jsii-rosetta worker count to default (aws#16755)
  fix: CDK does not honor NO_PROXY settings (aws#16751)
  ...
njlynch pushed a commit that referenced this pull request 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*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request 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*
mergify bot pushed a commit that referenced this pull request Jul 19, 2023
The AutoDeleteObjects Custom Resource should pass when the bucket doesn't exist.

With #16756 we introduced a safety check to only delete buckets that are marked for object-deletion.
This check would unintentionally bypass the special case to mark the CR deletion as successful when the bucket doesn't exist.
Additionally, with the upgrade to SDK v3 we need to change the check from `error.code` to check for the actual error instance.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
bmoffatt pushed a commit to bmoffatt/aws-cdk that referenced this pull request Jul 29, 2023
The AutoDeleteObjects Custom Resource should pass when the bucket doesn't exist.

With aws#16756 we introduced a safety check to only delete buckets that are marked for object-deletion.
This check would unintentionally bypass the special case to mark the CR deletion as successful when the bucket doesn't exist.
Additionally, with the upgrade to SDK v3 we need to change the check from `error.code` to check for the actual error instance.

----

*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
contribution/core This is a PR that came from AWS. p0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

‼️ s3: toggling off auto_delete_objects for Bucket empties the bucket
6 participants