AWS Python boto3描述所有区域中的所有实例并输出到CSV

问题描述 投票:0回答:2

我开始使用boto3,我想知道如何使用自定义属性获取所有区域中所有ec2实例的清单,并将其放入CSV文件。对于单个区域,它看起来很简单:

import boto3
import jmespath
import csv

client = boto3.client('ec2')
response = client.describe_instances()

myData = jmespath.search("Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, InstanceType, State.Name, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress, KeyName, [Tags[?Key=='Name'].Value] [0][0]]", response)

myFile = open('inventory.csv', 'w')
with myFile:
    writer = csv.writer(myFile)
    writer.writerows(myData)

但是我不知道如何在所有地区都取得相似的结果。我已经尝试过类似的事情:

all_regions = client.describe_regions()

RegionList = []
for r in all_regions['Regions']:
    RegionList.append(r['RegionName'])

for r in RegionList:
    client = boto3.client('ec2', region_name = r)
    response = client.describe_instances()
    myData = jmespath.search("Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, InstanceType, State.Name, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress, KeyName, [Tags[?Key=='Name'].Value] [0][0]]", response)

myFile = open('inventory.csv', 'w')
with myFile:
    writer = csv.writer(myFile)
    writer.writerows(myData)

但是我只得到一个空列表。

python amazon-web-services csv boto3
2个回答
1
投票

为我工作:

import csv
import jmespath
import boto3
import itertools
import configparser

config = configparser.ConfigParser()
config.read('/home/me/.aws/credentials')

ProfileList = config.sections()

client = boto3.client('ec2')
all_regions = client.describe_regions()

RegionList = []
for r in all_regions['Regions']:
    RegionList.append(r['RegionName'])

myData = []
for p in ProfileList:
    for r in RegionList:
        current_session = boto3.Session(profile_name = p, region_name = r)
        client = current_session.client('ec2')
        response = client.describe_instances()
        output = jmespath.search("Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, InstanceType, State.Name, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress, KeyName, [Tags[?Key=='Name'].Value] [0][0]]", response)
        myData.append(output)

output = list(itertools.chain(*myData))
with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerow(['AccountID','InstanceID','Type','State','AZ','PrivateIP','PublicIP','KeyPair','Name'])
    writer.writerows(output)

迭代遍历八个帐户和所有区域,但大约需要五分钟才能完成。为了进行比较,bash只需一分钟。有没有办法增加执行时间?


0
投票

尝试一下...

import boto3 

regions= [
    #'ap-east-1',
    'ap-northeast-1',
    'ap-northeast-2',
    'ap-south-1',
    'ap-southeast-1',
    'ap-southeast-2',
    'ca-central-1',
    'eu-central-1',
    'eu-north-1',
    'eu-west-1',
    'eu-west-2',
    'eu-west-3',
    #'me-south-1',
    'sa-east-1',
    'us-east-1',
    'us-east-2',
    'us-west-1',
    'us-west-2'
    ]

 for region_name in regions:
    print(f'region_name: {region_name}')
    ec2= boto3.resource('ec2', region_name=region_name)
    instances= ec2.meta.client.describe_instances()
    for instance in instances['Reservations']:
        print(instance)

希望有帮助干杯r0ck

© www.soinside.com 2019 - 2024. All rights reserved.