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.
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.
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?
The answer of your problem is already answered with this state, CloudFormation will not update the stack unless there is a change in properties of the resources already created in the stack.
And for the answer for your question, please check the explanation below.
There is a way to force Cloudformation to update the stack using the
AWS::CloudFormation::Init
. By using cfn-init, each instance can update itself when it detect the change that made byAWS::CloudFormation::Init
in metadata.There is a concept that we must understand first, that is the difference between UserData and metadata, at least under the
AWS::CloudFormation::Init
case.Userdata
: Will be only called once when the instance is being launch for the first time (this including update that need the instance to be replaced). So, if you update the stack (not creating a new one), even if you change the parameter value, it won't change anything if you call the parameter underUserData
.Metadata
: Can be updated anytime. To make it works, you have to make sure that the daemon that detect the metadata changed is running (the daemon is called the cfn-hup)If you already use the
Metadata
andAWS::CloudFormation::Init
, the data is not immediately being updated. As far I know, here is the condition the data to be change after change theMetadata
value.cfn-init
command again with it's parameterMetadata
is checking the change once in 15 minutes.Use the AWS CLI Update-Stack command. If you use the AWS CLI you can inject parameters into your stack so any change to any of the parameters result in a new stack. I do this myself to inject the Git/version commit ID into UserData so simply committing changes to the stack's JSON/Yaml to Git will allow stack updates. Any change to the parameters file will allow stack updates, even just a comment. I reference my Git commit ID in UserData the same way you are referencing Ref:CompanyName so when I change the Git commit ID the userData section is updated on stack updates.
Update Stack Command
Process
With this approach you make your parameters changes to the parameters json or yaml file then check it into version control. Now if you use a build server you can update your stack by checking out master and just running that one line above. Using AWS CodeBuild makes this easy so you don't need jenkins.
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:
Assume that I am using
db-name
passing it as value toDBInstanceIdentifier
.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 theError 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.