为什么我无法通过 python boto3 代码列出所有 EC2 属性?
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()
我无法返回 Excel 文件中的 block_device_mapping.device_name、block_device_mapping.device_status 的值。有人可以建议我缺少什么吗?我可以在实例中看到。all() 具有以下可以调用的属性: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html
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()
I am unable to return the values of block_device_mapping.device_name, block_device_mapping.device_status in the excel file. Can some one advice what is that I am missing. I can see in the instances.all() has following attributes that can be called: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ec2_resource.instances.all()
命令返回ec2.Instance
的列表。查看 EC2 的 boto3 文档:
ec2.Instance
对象包含一个名为block_device_mappings
的属性(注意它是复数),DeviceName
条目和Ebs
字典Ebs
字典包含一个名为Status
的条目,因此,有可能是多个块连接到 Amazon EC2 实例的设备。这不能很好地映射到每行具有相同列数的 CSV 文件 - 例如,考虑一下如果有多个 Amazon EBS 卷附加到 EC2 实例,则该文件应包含哪些信息。
要访问“名称”和“状态”字段,您可以循环访问
block_device_mappings
中的每个条目,也可以假设您只需要有关附加的第一个卷的信息。要获取有关第一个卷的信息,您可以简单地使用:
要循环访问每个卷,您可以使用:
The
ec2_resource.instances.all()
command is returning a list ofec2.Instance
.In looking at the boto3 documentation for EC2:
ec2.Instance
object includes an attribute calledblock_device_mappings
(note that it is plural)DeviceName
and anEbs
dictionaryEbs
dictionary contains an entry calledStatus
Therefore, there might be multiple block devices attached to an Amazon EC2 instance. This does not map well to a CSV file that expects the same number of columns for each row -- for example, think about what information it should contain if there are multiple Amazon EBS volumes attached to the EC2 instance.
To access the Name and Status fields, you could either loop through each entry in
block_device_mappings
, or you could make an assumption that you only want information about the first volume that is attached.To obtain information about the first volume, you could simply use:
To loop through each volume, you could use: