30

After cloud-init runs a user data script on the first boot of an EC2 instance, a state file is presumably written so that cloud-init won't run the script again on subsequent reboots. There are cases where I'd like to delete this state file so that the user data script will run again. Where is it?

Mike Conigliaro
  • 3,105
  • 2
  • 24
  • 24

4 Answers4

34

rm /var/lib/cloud/instances/*/sem/config_scripts_user

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.

rm /var/lib/cloud/instance/sem/config_scripts_user

Bruno Bronosky
  • 4,429
  • 3
  • 24
  • 32
Mike Conigliaro
  • 3,105
  • 2
  • 24
  • 24
  • 2
    You figured that out quickly. – c24w Jun 01 '17 at 09:19
  • 11
    @c24w Those timestamps are misleading. It actually took me a few hours of research and testing, so once I figured it out, I created this question to help the next person looking for a definitive answer. – Mike Conigliaro Jun 01 '17 at 17:21
  • 1
    I've come across [this](https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/) since and didn't realise it was encouraged to post wiki-type Q&As. Appreciate you documenting this! – c24w Jun 08 '17 at 20:11
  • 1
    I'm glad this was encouraged on serverfault. I've seen it downvoted many times on SO – Nathan Apr 11 '18 at 16:51
  • 1
    In addition to @Mike Conigliaro response, in order to delete only the running one semaphore: rm /var/lib/cloud/instances/$(curl -s http://169.254.169.254/latest/meta-data/instance-id)/sem/config_scripts_user – NicoKowe Jul 13 '18 at 14:39
  • 1
    @flag5 That `curl` to the metadata service to get the current instance id is unnecessary because `/var/lib/cloud/instance` is already a symlink to the current instance under `/var/lib/cloud/instances`. :-) – Mike Conigliaro Aug 13 '18 at 22:21
6

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.

Hardeep Singh
  • 374
  • 4
  • 11
  • 1
    Note: if are *replacing* an existing user script on an instance, you may need to run `cloud-init clean` on the instance before the new user-data script will execute. – crockeea Mar 27 '20 at 00:50
  • This method is documented by AWS here https://serverfault.com/questions/797482/how-to-make-ec2-user-data-script-run-again-on-startup – comfytoday Oct 14 '20 at 01:02
  • The AWS documentation is here: https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/ – Jordan Brough Jun 16 '22 at 12:48
2

You can put your script in /etc/rc.local, which will run the script on every reboot.

0

The below script worked perfectly for me after the ubuntu ec2 instance was launched.

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
sudo apt update
sudo apt install docker.io -y
sudo docker run -d -p 8080:80 --name web nginx
sudo docker ps
touch test
echo " " >> /home/ubuntu/.ssh/authorized_keys 
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCmTigIVKPSAIeJrkkrsKYyBaUrvs4f1E8iIl7cyIsgmeTN1IZ3JU5KVH7SOcOE10+HsDRzw0d7PiRmYXeuiDyQXPbim8/skiubRX/9OkIuil1iqjGKISSWw2I/HP+RQg881PNC7OZiRWhAY2zxpaLCBjyZ53jvcvBZ/mQ1/P+rvSPZ+pV6MR8mk5j4ijvswSs6RNikf3VCIxjp/tPYYkkCU/piOqsyofPDUyVepnHCv4oEuv4nru/q9ExiOY27XDM6bFxLiyuASnE4VO99LGnq8sM2hl2nWav0vTnydJNFKAVtQ4jPxyliHeIU+8iFn+0fxlQbDJCdLPHz+h1J1CHk3yMaV0vHh1fJEQp7MIdwihU9guPZRwvJLlzmc6uVsr6sqB9kfJ3rBWYumYMh5PwBXfHl2on2y+Hu73MWn1XlR0Asd9cifU9lOagA8iovbRluR5wqUwCZpSjcTVRXZYAhs/AQVzo8BhP+x8fl1/kggsE71Z2OsjpWsCeUJXRr0ph//bVRXFz2pO5qnyFQoaXpoTYPPEHA9mHOHrIxp0IN+EvD7gaC6O84tk3RIYlVF1Eg5MNBL+vlf1EDkcC6ZyVFkQV58ooxZ3OCcHu69ipnJj0PyBwjlPOnpLhrQ+7D083QcypcXqzOzjp0/9yWsNrsBjX3WZboX0nhbJLDnkwYwQ==" >> /home/ubuntu/.ssh/authorized_keys 

cat /home/ubuntu/.ssh/authorized_keys 
--//

devops-admin
  • 119
  • 2