2

I'm following this tutorial: https://help.ubuntu.com/community/EC2StartersGuide

To start an instance, you run:

ec2-run-instances ami-xxxxx -k ec2-keypair

Then run:

ec2-describe-instances

which gets you the external host name of the instance.

And later, to ssh, you run:

ssh -i /path/to/ec2-keypair.pem ubuntu@<external-host-name>

This works fine, but here is my question:

How can I automate this in a bash script? Can I somehow parse the response returned from "ec2-describe-instances"?

user94154
  • 16,176
  • 20
  • 77
  • 116

1 Answers1

0

I don't know what the output of ec2-describe-instances looks like, but if it's simply the hostname, then you should be able to do:

host=$(ec2-describe-instances)
ssh -i /path/to/ec2-keypair.pem ubuntu@$host
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • It's not. I found http://stackoverflow.com/questions/2644742/getting-id-of-an-instance-newly-launched-with-ec2-api-tools and http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/ to be quite helpful. I've been using Linux for about 6 months and have only recently begun learning bash scripting. – user94154 Jan 13 '11 at 21:33
  • @user: The SO question you linked to shows the output of `ec2-run-instances`. Is the output of `ec2-describe-instances` the same? Similar? Can you show it? Which field are you after? Do [any of the filters](http://docs.amazonwebservices.com/AWSEC2/latest/CommandLineReference/index.html?ApiReference-cmd-DescribeInstances.html) help narrow the information down so less parsing has to be done? – Dennis Williamson Jan 14 '11 at 00:44
  • external_dns=$(ec2-describe-instances $instance_id | awk '/INSTANCE/{print $4}'). You need to make sure the instance has been deployed, though, or else it will return "pending". – user94154 Jan 14 '11 at 15:23
  • @user: So have you answered your own question or did I? If you did, then you should post an answer and self-accept it. – Dennis Williamson Jan 14 '11 at 16:10