Run a bash script after EC2 instance boots

I'd suggest just using the user-data option to ec2-run-instances. It lets you give a script of some sort to the VM which will be run on first boot. If you're using ubuntu or debian, you can use cloud-init, which puts some nice polish on the process. If using cloud-init, you can use the [runcmd] section of the config file to specify arbitrary commands to run after boot.

Thanks to SF user Eric Hammond for the user-data page. Check out his site - it has a wealth of information on AWS.

Edit: After re-reading, it's not clear whether you wanted to run a command on initial boot or on every boot. The above instructions only apply to the initial boot. If you want to run a command on every boot, you have a couple options - you can run a command via the @reboot cron directive, or alternatively you can add the script to /etc/rc.local, which will be run each time the system boots.


If you were using an AMI that had the cloud-init package installed (like Amazon Linux or Ubuntu) then you could simply pass the bash script (which starts with #!) as the user-data-file and it would run automatically at the end of the boot process.

For example, it could be as simple as:

ec2-run-instances                             \
  --user-data-file /home/root/beginProcess.sh \
  --key $USER                                 \
  ami-XXXXXXXX

Here's the article where I introduced the user-data script concept, now available in major EC2 AMIs like Amazon Linux and Ubuntu: http://alestic.com/2009/06/ec2-user-data-scripts

Unfortunately, it looks like you're wanting to use a RHEL AMI. I ran a copy of that and could not find any references to cloud-init or running user-data scripts on first boot, nor did a test of the same work.

I'm not saying you should switch Linux distros just for this, but Amazon Linux is based on RHEL, so that might work for you.

Here's an article I wrote that might help you debug your user-data script if it doesn't work the first time: http://alestic.com/2010/12/ec2-user-data-output


This is an old question but I could not find a good answer to this anywhere. My use case was needing to re-run some commands on an existing instance due to the instance being shut down and started up again on a schedule.

The typical answer is to use EC2 User Data, but this doesn't seem to work for me. Either it's because User Data is only for running commands on newly created instances (such as bootstrapping instances for scaling), or because the documentation is out of date. Not sure which, but I was having no luck getting anything to work. There is apparently some method for running commands each time an existing instance starts shown here: How to make EC2 user data script run again on startup?, but this simply doesn't work for me at all. Nothing happens.

After bumbling around for a while, I took a shot in the dark and just modified the .bashrc file on my EC2 instance, and lo and behold, it works exactly as expected and runs my commands on instance startup. This is almost too obvious, but it's a little remarkable it's not mentioned anywhere.


The authoritative source for how to use EC2 UserData to run a script each time your instance boots is here. In short, I create a script on the instance (make sure to make it executable), then I use the following userdata script:

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"

/path/to/script.sh
--//

You only need to change the second-to-last line to point to your script.

Another trick I found is that if you were already playing around with this and had some other UserData script on the instance, it is cached on the machine and does not update when you change the UserData script. To force the new UserData script to actually be used, you need to run cloud-init clean on the instance to clear the UserData cache.