Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart an existing Amazon EC2 instance with a user-specified script?

While I've found plenty of documentation on how to create new instances of an an Amazon EC2 AMI with a user-data script (ala Eric Hammond), I've not seen a way to start an already existing, but currently stopped, Amazon EC2 instance with a user-specified script upon restarting it.

I have a number of Amazon EC2 Ubuntu instances that are used for testing and disaster recovery. I'd like to be able to start one up and have it, for example, automatically fetch and restore a backup that corresponds to either our staging or production environment, as needed. I'd rather not have to maintain two separate instances each with its own Elastic IP.

Is there any way to do this from the command line with Amazon ec2tools similar to the way it can be done with ec2-run-instances command? Basically I want to be able to pass a script that will run as root as if it had been called from rc.local or similar.

like image 209
Michael Oryl Avatar asked Dec 10 '12 20:12

Michael Oryl


1 Answers

  1. You can do it with the help of API Tools. Check ec2-modify-instance-attribute command for --user-data property.

  2. Also, AWS Console allows you to do the same thing on stopped instance: enter image description here

  3. Alternatively, you can always run a command or script via ssh as the last argument:

    ssh [user@]hostname [command]

like image 175
Roman Newaza Avatar answered Oct 09 '22 08:10

Roman Newaza