Arafat Nalkhande
Arafat Nalkhande

Reputation: 11708

How to ensure Resource deletion/creation order during AWS Cloudformation Update

My use case is that we already have a stack created out of AWS Cloudformation.

Now I want to update that stack and my requirement is that I want to delete a resource that was already created and add the new modified resource but I want to make sure that the delete happens before the create part.

I explored the dependsOn but that helps me with setting the order of resource creation. It doesn't help with ensuring the delete and the create ordering (or atleast nothing that i could find)

How to make sure that the resource deletion happens before resource creation while doing the cloudformation update

Upvotes: 11

Views: 9115

Answers (3)

RonenD
RonenD

Reputation: 63

What you are looking for here is actually the default behavior for Terraform, which will be to delete the resource and only then create the modified one.

With Terraform you can also change this to align with the CloudFormation style - create the modified resource and only then delete the old one: Terraform Lifecycle capability (block) with attribute create_before_destroy = true, but unfortunately this is not available with CloudFormation (or CDK).

Upvotes: 1

Felipe Desiderati
Felipe Desiderati

Reputation: 2982

There is no other option unless dividing your operation into two steps:

  1. You will need to update the stack, using the current template, changing only the name of the resources which you will modify later.

  2. Update the stack - uploading a new template with your modifications - but remember to set the resource name with the previous value.

Upvotes: 3

Darshan Ambhaikar
Darshan Ambhaikar

Reputation: 785

I understand you want to,

delete a resource that was already created and add the new modified resource

Below is my understanding, let me know if it helps,

It is very trickey to Delete and Create resource having same resource name/dependency in a single CloudFormation deployment.

Easiest approach :

  1. First deploy CFN template to Delete a resource i.e. remove the code from template and than add new resource/modified one. While doing that you need to check if "retention policy" is in place because if you are retaining deleted resources than CloudFormation will not create same resource again.
  2. Than deploy the CFN template to create/modify resources

Other approach might be:

  1. If you want to ensure the resource deletes before creating new one, in a single template, you might need to create a nested stack for resource deletion and resource creation
  2. And add dependency on the deletion cloudFormation template i.e. Create resource template will depends on Delete resource template. There are also AWS::CloudFormation::WaitCondition which can be used here.

Also I think anyway you will receive an error if you try to create/modify on deleted/ delete in-progress resource

Upvotes: 2

Related Questions