Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a resource without deleting it during a cloudformation stack update

I have a cloudformation template that creates an S3 bucket as part of a cloudformation stack. On the new version of my template, I 'm planning to migrate my application from S3 to EFS.

Is there a way to remove the S3 bucket resource from the template, without having it deleted? Ideally, I would like my older users to have the s3 bucket available after they upgrade, but for the new users to not have it at all. It looks like DeletionPolicies could help here, but the documentation on it says that it only applies to stack deletion, but not upgrades.

like image 767
Ramiro Berrelleza Avatar asked Apr 04 '17 17:04

Ramiro Berrelleza


3 Answers

I came across this question requiring a slight variation. I needed to extract my bucket to another stack and can not delete it in the move. This method worked well:

  1. create a new stack with the bucket in question. (note: you now have 2 stacks referencing the same bucket)
  2. remove the bucket from the original stack. The resource is deleted from the original stack but not from S3 since it is still referenced in your new stack.

I also tested Houser's response above and confirmed the bucket will not be deleted if it contains files. While this works, it does attempt to delete the bucket 3 times before it completes (and reports errors each time). migrating to a new stack will not throw any errors.

like image 24
riiich Avatar answered Sep 26 '22 07:09

riiich


Going to elaborate on user3470009's answer.

The main, advertised purpose of the DeletionPolicy is to keep a resource when a stack is deleted. It's mentioned almost as an afterthought in the AWS docs for DeletionPolicy that it also functions during resource removal from a stack:

Note that this capability also applies to stack update operations that lead to resources being deleted from stacks. For example, if you remove the resource from the stack template, and then update the stack with the template.

So the workflow to remove a resource from a stack without deleting the actual resource is:

  1. Add "DeletionPolicy" : "Retain" to the resource declaration in your CF template
  2. Apply changes by either saving in the UI or running aws cloudformation on the CLI or whatever other tool you use
  3. Check in the UI that your resource has the correct changes. There are some gotchas about when CF doesn't update the metadata. See the docs link above
  4. Remove the resource from your template
  5. Apply changes. Watch the events log to see that it says DELETE_SKIPPED:

2018-10-15T15:32:32.956Z HostedZone DELETE_SKIPPED

like image 120
Indigenuity Avatar answered Sep 23 '22 07:09

Indigenuity


Setting a DeletionPolicy of "Retain" will cause the bucket itself to remain after a stack update that deletes the resource.

like image 42
user3470009 Avatar answered Sep 25 '22 07:09

user3470009