如何使用带 python 的 boto 遍历区域?

How do I loop through regions using boto with python?

Python新手.. 我正在尝试编写一个脚本,该脚本将使用我提供的密钥连接到我的 AWS 帐户,然后它将列出其中的实例,并将通过遍历每个区域来实现。所以我希望它连接并从一个区域和列表开始,然后转到下一个区域和列表,依此类推。 如果我注释掉以下行,下面的代码将起作用,但它仅适用于 us-east-1,这是我盒子上 AWS 凭证文件中的默认区域。 “地区 = boto.ec2.regions() 对于区域中的 x:" 如果我取消注释这些行,脚本将为每个区域 return 我的所有实例一次。所以我最终得到了一个巨大的列表,其中包含一遍又一遍重复的相同实例。我错过了什么会让我做我想做的事?放轻松,我是 Python 的新手。 谢谢

import boto.ec2
import os
import sys

ACCESS_KEY = raw_input("Enter your access key > ")
SECRET_KEY = raw_input("Enter your secret key > ")
if not ACCESS_KEY:
    sys.exit("ERROR: You did not enter anything for ACCESS KEY or SECRET KEY. Exiting...")

ec2_conn = boto.connect_ec2(ACCESS_KEY, SECRET_KEY)

regions = boto.ec2.regions()
for x in regions:
    reservations = ec2_conn.get_all_reservations()
    for r in reservations:
        for i in r.instances:
            print r.instances
            print 'Tags: ',i.tags['Name']
            print 'Public IP Address: ',i.ip_address
            print 'Private IP address: ',i.private_ip_address
            if (i.virtualization_type == 'hvm'):
                platform = 'Windows'
            else:
                platform = 'Linux'
                print 'Platform: ',platform
            print 'State: ',i.state
            print

boto.connect_ec2移动到第一个for循环中,并将区域传递给它。像这样:

import boto.ec2
import os
import sys

ACCESS_KEY = raw_input("Enter your access key > ")
SECRET_KEY = raw_input("Enter your secret key > ")
if not ACCESS_KEY:
    sys.exit("ERROR: You did not enter anything for ACCESS KEY or SECRET KEY. Exiting...")

regions = boto.ec2.regions()
for x in regions:
    ec2_conn = boto.connect_ec2(ACCESS_KEY, SECRET_KEY, region=x)
    reservations = ec2_conn.get_all_reservations()
    for r in reservations:
        for i in r.instances:
            print r.instances
            print 'Tags: ',i.tags['Name']
            print 'Public IP Address: ',i.ip_address
            print 'Private IP address: ',i.private_ip_address
            if (i.virtualization_type == 'hvm'):
                platform = 'Windows'
            else:
                platform = 'Linux'
                print 'Platform: ',platform
            print 'State: ',i.state
            print