7

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Michael Oryl
  • 20,856
  • 14
  • 77
  • 117

4 Answers4

2
  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]

Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
  • The issue here is that, it appears, the scripts placed in the user data area are run **only** when an instance is being first created from an AMI, which is likely why there is no user-data parameter in the ec2-start-instances command even though it is present in the ec2-run-instances command. The SSH method certainly is viable, but it doesn't answer the question about being able to do it with ec2tools and it would require some sort of authentication for an account on the starting instance, though I guess that's no big deal since we're using the key files. – Michael Oryl Dec 11 '12 at 12:37
  • I have just completed simple user data test and can see it is not executed after reboot or stop / start. It looks to me strange it is possible to change it, but it will not be executed. I will contact Amazon about that. BTW, Take a look at this page - it might be also interesting for you: https://help.ubuntu.com/community/CloudInit – Roman Newaza Dec 12 '12 at 02:55
1

You may consider to Stop/Restart the Functionality of Amazon EC2 Instance using AWS Command Line Interface as described here.

Without Elastic Load Balancer

aws ec2 stop-instances --instance-ids i-1a2b3c4d
aws ec2 start-instances --instance-ids i-1a2b3c4d

With Elastic Load Balancer without Autoscaling

aws elb deregister-instances-from-load-balancer --load-balancer-name elb-name --instances i-12ab34cd
aws elb register-instances-from-load-balancer --load-balancer-name elb-name --instances i-12ab34cd

Elastic Load Balancer with Autoscaling

aws autoscaling update-auto-scaling-group --auto-scaling-group-name "asg-name"  --launch-configuration-name "launch-config-name" --min-size 0 --max-size 0
aws autoscaling update-auto-scaling-group --auto-scaling-group-name "asg-name"  --launch-configuration-name "launch-config-name" --min-size 1 --max-size 1
eQ19
  • 9,880
  • 3
  • 65
  • 77
0

Have you looked into cloud formation templates? http://docs.amazonwebservices.com/AWSCloudFormation/latest/UserGuide/Welcome.html

I would imagine it would be straightforward to run your scripts on instance restart with this. I did this for setting up sharepoint server farm on AWS and investigated the CF scripts. http://aws.amazon.com/articles/9982940049271604

I have not tested this particular scenario but I believe this should give the required support

Keshi
  • 906
  • 10
  • 23
0

Sadly, the answer appears to be that it is not currently possible to do what I have asked.

Michael Oryl
  • 20,856
  • 14
  • 77
  • 117