社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

为什么我不能通过python boto3代码列出所有EC2属性?

Shubham Mishra • 4 年前 • 1627 次点击  
import boto3
import csv
import pprint

ec2_cli=boto3.client(service_name='ec2')   #creating a ec2_cli object with client session
aws_regions = ec2_cli.describe_regions()['Regions']  
all_aws_regions = []
for each_region in aws_regions:
    #print(each_region['RegionName'])
    all_aws_regions.append(each_region['RegionName'])

#print (all_aws_regions)

file_open = open('ec2_inventory.csv', 'w', newline='')
data_obj=csv.writer(file_open)
data_obj.writerow(["S.no", "InstanceID", "ImageID", "Instance Lifecycle", "Instance Type", "Private DNS Name", "Private IP Address", "Root Device Name", "Root Device Type", "VPC ID"])

count = 1
for each_region in all_aws_regions:
    ec2_resource = boto3.resource(service_name='ec2', region_name = each_region)
    for each_inst_in_reg in ec2_resource.instances.all():
        #print (count,each_inst_in_reg.instance_id, each_inst_in_reg.`block_device_mapping.device_name`, each_inst_in_reg.block_device_mapping.status, each_inst_in_reg.block_device_mapping.volume_id, each_inst_in_reg.dns_name, each_inst_in_reg.image_id, each_inst_in_reg.instance_lifecycle, each_inst_in_reg.instance_state_name, each_inst_in_reg.instance_type, each_inst_in_reg.ip_address, each_inst_in_reg.owner_id, each_inst_in_reg.private_dns_name, each_inst_in_reg.private_ip_address, each_inst_in_reg.root_device_name, each_inst_in_reg.root_device_type, each_inst_in_reg.vpc_id, each_inst_in_reg.tag_key)
        data_obj.writerow([count,each_inst_in_reg.instance_id, each_inst_in_reg.image_id, each_inst_in_reg.instance_lifecycle, each_inst_in_reg.instance_type, each_inst_in_reg.private_dns_name, each_inst_in_reg.private_ip_address, each_inst_in_reg.root_device_name, each_inst_in_reg.root_device_type, each_inst_in_reg.vpc_id])
        count+=1

file_open.close()

我无法返回block_device_mapping的值。设备名称,块设备映射。excel文件中的设备状态。谁能告诉我我错过了什么。我可以从这些例子中看出。all()具有以下可调用的属性: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/134150
文章 [ 1 ]  |  最新文章 4 年前