Ale*_*sey 2 python csv amazon-web-services boto3
我开始使用 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)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何为所有地区实现类似的结果。我尝试过这样的事情:
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)
Run Code Online (Sandbox Code Playgroud)
但我只得到一个空列表。
这对我来说工作:
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)
Run Code Online (Sandbox Code Playgroud)
它会遍历八个账户和所有区域,但大约需要五分钟才能完成。相比之下,bash 只需要一分钟。有没有办法提高执行速度?
| 归档时间: |
|
| 查看次数: |
10033 次 |
| 最近记录: |