could-be-anyone
could-be-anyone

Reputation: 141

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.

Upvotes: 14

Views: 3187

Answers (1)

Oscar De León
Oscar De León

Reputation: 300

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

Upvotes: 2

Related Questions