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.

Leave a Comment