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

Question:

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

Answer:

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

Leave a Reply