How to make EC2 user data script run again on startup?
Confirmed working on:
- CentOS 7.4
- Ubuntu 14.04
- Ubuntu 16.04
For the sake of completeness, if you have a situation where you care to keep track of the fact/possibility that this AMI [had a parent AMI that ...] and they all ran
cloud-init user data, you can delete only the current semaphore.
You can also configure your user data to re-run on every boot, instead of removing the state file. You have to use
cloud_final_modules in your userdata script to re-run the userdata script and for that you have to customise uderdata to have miultiple files in userdata. Example userdata file would be like:
Content-Type: multipart/mixed; boundary="//" MIME-Version: 1.0 --// Content-Type: text/cloud-config; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cloud-config.txt" #cloud-config cloud_final_modules: - [scripts-user, always] --// Content-Type: text/x-shellscript; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="userdata.txt" #!/bin/bash /bin/echo "Hello World" >> /tmp/userdata-test.txt --//
This will make userdata script to execute on last step of every boot process. Here only a single line
bin/echo "Hello World" >> /tmp/userdata-test.txt to be executed, replace this with your shell script that needs to be executed every time a machine is booted.
You can put your script in /etc/rc.local, which will run the script on every reboot.