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.
You can do it with the help of API Tools. Check ec2-modify-instance-attribute command for --user-data
property.
Also, AWS Console allows you to do the same thing on stopped instance:
Alternatively, you can always run a command or script via ssh as the last argument:
ssh [user@]hostname [command]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With