Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a CloudFormation stack to update when the parameter is updated?

I am running a AWS CloudFormation stack that takes in some parameters and launches EC2 instances along with other AWS resources. The parameters are fed into the user data of the EC2 instance and based on that changes are made dynamically to the web application residing on the EC2 instance.

CFN Parameters

UserData: 
      Fn::Base64: 
        Fn::Join: 
          - ""
          - 
            - "#!/bin/bash \n"
            - "sh website-conf/website_mysql_config.sh "
            - " -c \""
            - 
              Ref: "CompanyName"

As shown in the example above, CompanyName is one of the many parameters passed to the userdata script. The problem is, when any one or multiple of parameters are updated, CloudFormation does not detect that and instead throws this error.

CFN Error

So, in order to update the stack, I have to edit the stack and make changes to the ASG so that CloudFormation 'sees' the changes and executes the stack update.

Is there a way to force CFN to update the stack when the parameters are updated?

like image 872
captainblack Avatar asked Oct 19 '17 06:10

captainblack


People also ask

Can we update the stack after it has been created?

To update the stack from the AWS Management Console On the CloudFormation dashboard, choose the stack you created previously, and then choose Update Stack. In the Update Stack wizard, on the Select Template screen, select Use current template, and then choose Next.

What happens when a CloudFormation stack is updated?

When you directly update a stack, you submit changes and AWS CloudFormation immediately deploys them. Use direct updates when you want to quickly deploy your updates. With change sets, you can preview the changes AWS CloudFormation will make to your stack, and then decide whether to apply those changes.


1 Answers

CloudFormation will not update the stack unless there is a change in properties of the resources already created in the stack.

For example: Consider I have a simple template to create a database where I need to pass 2 parameters:

  1. db-name
  2. region

Assume that I am using db-name passing it as value to DBInstanceIdentifier.

Also assume that I am not using the input parameter region for any purpose in creation of resources (or its properties) of the stack in any way.It is more of a dummy parameter I keep for readability purpose.

I passed (TEST-DB1, us-east-1) as input parameters to the CloudFormation template and successfully created the resources.

Scenario-1: Now if I update the stack(still using the existing template) and just change the input parameters to (TEST-DB2, us-east-1). ie: changing just the db-name and not the region. Then CloudFormation will detect that, this parameter update, results in change in properties of running resource(s) of the stack and will compute and display the modifications as a change set.

Scenario-2: Suppose I make another update(still using the existing template) property and just change the input parameters to (TEST-DB1, us-east-2). ie: changing just the region and not the db-name. Then CloudFormation will detect that, this parameter update, result in NO change in properties of running resource(s) of the stack will show the Error creating change set.

Bottomline: Your change in input parameter must result in an update/replacement of any resources(or its attributes like security-groups,port etc..) of the stack. Then AWS CloudFormation will display them as Change Sets for your review. Also, the method (update or replacement) AWS CloudFormation uses depends on which property you update for a given resource type.

Your parameter "CompanyName" is not making any changes to the running resources of the stack. Hence it is reporting as Error creating change set. You need to use it to create any resource/resource properties of the stack. Then CloudFormation will detect the change-sets when you modify it. The same applies for any other input-parameters which you use.

like image 121
Madhukar Mohanraju Avatar answered Sep 20 '22 12:09

Madhukar Mohanraju