简体   繁体   English

AWS Python boto3 描述所有区域中的所有实例和 output 到 CSV

[英]AWS Python boto3 describe all instances in all regions and output to CSV

I'm starting using boto3 and I wonder how I can get an inventory of all ec2 instances in all regions with custom attributes and put it to CSV file.我开始使用 boto3,我想知道如何获取所有区域中具有自定义属性的所有 ec2 实例的清单,并将其放入 CSV 文件。 For the single region it looks simple:对于单个区域,它看起来很简单:

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)

But I can't figure out how to achieve a similar result for all regions.但我无法弄清楚如何为所有地区实现类似的结果。 I've tried something like this:我试过这样的事情:

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)

But I'm getting just an empty list.但我得到的只是一个空列表。

This work for me:这对我有用:

import csv
import jmespath
import boto3
import itertools
import configparser
import os

# Get list of profiles
config = configparser.ConfigParser()
path = os.path.join(os.path.expanduser('~'), '.aws/credentials')
config.read(path)
profiles = config.sections()

# Get list of regions
ec2_client = boto3.client('ec2')
regions = [region['RegionName']
            for region in ec2_client.describe_regions()['Regions']]

# Get list of EC2 attributes from all profiles and regions
myData = []
for profile in profiles:
    for region in regions:
        current_session = boto3.Session(profile_name = profile, region_name = region)
        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)

# Write myData to CSV file with headers
output = list(itertools.chain(*myData))
with open("ec2-inventory-latest.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerow(['AccountID','InstanceID','Type','State','AZ','PrivateIP','PublicIP','KeyPair','Name'])
    writer.writerows(output)

It iterates through eight accounts and all regions but it takes about five minutes to complete.它遍历八个帐户和所有区域,但大约需要五分钟才能完成。 For comparison, it takes only one minute on bash.作为对比,在 bash 上只需要一分钟。 Is there a way to increase execution time speed?有没有办法提高执行时间的速度?

try this...尝试这个...

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)

Hope it helps希望能帮助到你
Cheers干杯
r0ck摇滚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM