【原创】导出aws ec2为csv

1、安装 pip  install boto3 csv

2、使用脚本更新秘钥和地区

# 导出aws ec2列表为cvs
import boto3
import csv
 
ec2 = boto3.client(
    'ec2',
    aws_access_key_id="<key_id>",
    aws_secret_access_key="<秘钥>",
    region_name='地区',
    )
 
# Retrieves all regions/endpoints that work with EC2
# response = ec2.describe_regions()
# print('Regions:', response['Regions'])
 
# Retrieves availability zones only for region of the ec2 object
# response = ec2.describe_availability_zones()
# print('Availability Zones:', response['AvailabilityZones'])
 
response = ec2.describe_instances()
with open ("东京.csv""w", newline="") as csvf:
    writer = csv.writer(csvf)
    csv_head = ["创建时间""外网IP""内网IP""地区""Name"]
    writer.writerow(csv_head)
    for in response["Reservations"]:
        for in i['Instances']:
            if 'PublicIpAddress' not in j:
                j['PublicIpAddress'= ""
            if 'Tags' not in j:
                j['Tags'= []
            for dic in j['Tags']:
                k, v = dic.values()
            row_cvs = [j['LaunchTime'], j['PublicIpAddress'], j['PrivateIpAddress'], 'ap-east-1', v]
            writer.writerow(row_cvs)
 
            print(j['LaunchTime'], j['PublicIpAddress'], j['PrivateIpAddress'], 'ap-east-1', v)
posted @ 2019-10-11 20:59  shuyang  阅读(1184)  评论(0编辑  收藏  举报