Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force EC2 Instance Replacement When Updating UserData in CloudFormation

I'm using the UserData field in a CloudFormation template to install software on the instance. When I update the UserData I would like CloudFormation to replace the instance. However, UserData is not a replace field and as a result the instance is stopped but not replaced and the new UserData script is never run.

Is there a way I can update the UserData with CloudFormation?

I'm referring to this documentation.

like image 636
could-be-anyone Avatar asked Dec 12 '19 15:12

could-be-anyone


People also ask

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.

How do I force update CloudFormation stack?

Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . In the AWS CloudFormation console , from the list of stacks, select the running stack that you want to update. In the stack details pane, choose Update.


1 Answers

As you can see in the documentation, updating user data doesn't necessarily (and in most cases shouldn't) require replacing the instance.

You could try and run the updated User Data script from a Systems Manager command. The script should be stored in /var/lib/cloud/instances/[instance-id]/user-data.txt

OR you can set it to run after rebooting. Check out the guide at https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/ , and more examples at https://cloudinit.readthedocs.io/en/latest/topics/examples.html

like image 88
Oscar De León Avatar answered Sep 21 '22 19:09

Oscar De León