【问题标题】:Do any AWS API scripts exist in a repository somewhere?某个存储库中是否存在任何 AWS API 脚本?
【发布时间】:2013-11-26 00:01:42
【问题描述】:

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

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

谢谢

【问题讨论】:

  • 创建此“相关”字段后,我得到了这个很好的答案:stackoverflow.com/questions/2644742/…
  • IMO 最好直接在真正的编程语言中使用 API。命令行最适合手动交互或更简单的脚本。并不是说你不能,只是它可能不那么可靠而且更难。
  • 当你说一种真正的语言时,你的意思是(例如)通过Java?这很有趣,我见过的大多数示例都是 bash 脚本。感谢您的洞察力
  • 也许我最近写bash 脚本太烦了,这就是我忍不住回答的原因。基本上,您可以使用任何语言来查询 Web 服务。只是当您的项目增长并且您需要为脚本添加更多可靠性和错误检测功能时,bash 成为一种负担。但总是很想快速破解一些东西。

标签: amazon-web-services amazon-ec2 ec2-api-tools


【解决方案1】:

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

【讨论】:

  • 谢谢,太棒了
【解决方案2】:

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

使用 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

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 2016-04-03
    • 2017-05-21
    • 2013-04-27
    • 2015-07-27
    • 1970-01-01
    • 2015-09-13
    • 2020-11-06
    • 2011-11-23
    相关资源
    最近更新 更多