Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I have an EC2 instance running an AMI based on the Amazon Linux AMI. Like all such AMIs, it supports the cloud-init system for running startup scripts based on the User Data passed into every instance. In this particular case, my User Data input happens to be an Include file that sources several other startup scripts:

#include

http://s3.amazonaws.com/path/to/script/1

http://s3.amazonaws.com/path/to/script/2

The first time I boot my instance, the cloud-init startup script runs correctly. However, if I do a soft reboot of the instance (by running sudo shutdown -r now, for instance), the instance comes back up without running the startup script the second time around. If I go into the system logs, I can see:

Running cloud-init user-scripts

user-scripts already ran once-per-instance

[  OK ]

This is not what I want -- I can see the utility of having startup scripts that only run once per instance lifetime, but in my case, these should run every time the instance starts up, like normal startup scripts.

I realize that one possible solution is to manually have my scripts insert themselves into rc.localafter running the first time. This seems burdensome, however, since the cloud-init and rc.d environments are subtly different and I would now have to debug scripts on first launch and all subsequent launches separately.

Does anyone know how I can tell cloud-init to always run my scripts? This certainly sounds like something the designers of cloud-init would have considered.

1 Answer

0 votes
by (44.4k points)

In 11.10, 12.04 and later, you'll attain this by creating the 'scripts-user' run 'always'. In /etc/cloud/cloud.cfg you'll see something like:

cloud_final_modules:

- rightscale_userdata

- scripts-per-once

- scripts-per-boot

- scripts-per-instance

- scripts-user

- keys-to-console

- phone-home

- final-message

This can be changed after boot, or cloud-config data overriding this stanza will be inserted via user-data. Ie, in user-data you can provide:

#cloud-config

cloud_final_modules:

- rightscale_userdata

- scripts-per-once

- scripts-per-boot

- scripts-per-instance

- [scripts-user, always]

- keys-to-console

- phone-home

- final-message

That can also be '#included' as you've done in your description. Unfortunately, right now, you cannot modify the 'cloud_final_modules' but only override it. I hope to feature the ability to change config sections at some point.

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...