-1

I want to start 10 instances, get their instance id's and get their private IP addresses.

I know this can be done using AWS CLI, I'm wondering if there are any such scripts already written so I don't have to reinvent the wheel.

Thanks

Aman Chawla
  • 704
  • 2
  • 8
  • 25
  • After creating this the 'related' field led me to this pretty good answer: http://stackoverflow.com/questions/2644742/getting-id-of-an-instance-newly-launched-with-ec2-api-tools?rq=1 – Aman Chawla Nov 21 '13 at 08:16
  • IMO it's better to use the API directly in a real programming language. Command line is suited best for manual interaction or simpler scripts. Not that you can't, it simply might be less reliable and harder. – akostadinov Nov 21 '13 at 08:18
  • When you say a real language, you mean (for example) through Java? That's interesting, most of the examples I've seen have all been bash scripts. Thanks for the insight – Aman Chawla Nov 21 '13 at 08:20
  • maybe I am just too annoyed writing `bash` scripts recently and that's why I couldn't resist to answer. Basically you can use any language to query the web services. It's just that when your project grows and you need to add more reliability and error detection features to your scripts, `bash` becomes of a burden. But it's always tempting to quickly hack something together with it. – akostadinov Nov 21 '13 at 08:26

2 Answers2

1

I recommend to use python and boto package for such automation. Python is more clear than bash. You can user following page as starting point: http://boto.readthedocs.org/en/latest/ec2_tut.html

Vadym Fedorov
  • 2,395
  • 2
  • 21
  • 31
1

In the off chance that someone in the future comes across my question, I thought I'd give my (somewhat) final solution.

Using python and the Boto package that was suggested, I have the following python script.

It's pretty well commented but feel free to ask if you have any questions.

import boto
import time
import sys

IMAGE           = 'ami-xxxxxxxx' 
KEY_NAME        = 'xxxxx'
INSTANCE_TYPE   = 't1.micro'
SECURITY_GROUPS = ['xxxxxx'] # If multiple, separate by commas
COUNT           = 2 #number of servers to start

private_dns     = [] # will be populated with private dns of each instance

print 'Connecting to AWS'
conn = boto.connect_ec2()

print 'Starting instances'
#start instance
reservation = conn.run_instances(IMAGE, instance_type=INSTANCE_TYPE, key_name=KEY_NAME, security_groups=SECURITY_GROUPS, min_count=COUNT, max_count=COUNT)#, dry_run=True)

#print reservation #debug

print 'Waiting for instances to start'
# ONLY CHECKS IF RUNNING, MAY NOT BE SSH READY
for instance in reservation.instances: #doing this for every instance we started
    while not instance.update() == 'running': #while it's not running (probably 'pending')
        print '.', # trailing comma is intentional to print on same line
        sys.stdout.flush() # make the thing print immediately instead of buffering
        time.sleep(2) # Let the instance start up
print 'Done\n'

for instance in reservation.instances:
    instance.add_tag("Name","Hadoop Ecosystem") # tag the instance
    private_dns.append(instance.private_dns_name) # adding ip to array
    print instance, 'is ready at', instance.private_dns_name # print to console

print private_dns
Aman Chawla
  • 704
  • 2
  • 8
  • 25