-1

我想启动 10 个实例,获取它们的实例 ID 并获取它们的私有 IP 地址。

我知道这可以使用 AWS CLI 来完成,我想知道是否已经编写了任何这样的脚本,所以我不必重新发明轮子。

谢谢

4

2 回答 2

1

如果将来有人遇到我的问题,我想我会给出我的(有点)最终解决方案。

使用 python 和建议的 Boto 包,我有以下 python 脚本。

评论很好,但如果您有任何问题,请随时询问。

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
于 2013-11-25T16:01:42.807 回答
1

我建议使用 python 和 boto 包来实现这种自动化。Python 比 bash 更清晰。您可以使用以下页面作为起点:http ://boto.readthedocs.org/en/latest/ec2_tut.html

于 2013-11-22T16:39:31.073 回答