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

发布于 2025-01-11 20:24:36 字数 2153 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半窗疏影 2025-01-18 20:24:36

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 中的每个条目,也可以假设您只需要有关附加的第一个卷的信息。

要获取有关第一个卷的信息,您可以简单地使用:

each_inst_in_reg.block_device_mappings[0].DeviceName
each_inst_in_reg.block_device_mappings[0].Ebs['Status']

要循环访问每个卷,您可以使用:

for volume in each_inst_in_reg.block_device_mappings:
  name = volume.DeviceName
  status = volume.Ebs['Status']

The ec2_resource.instances.all() command is returning a list of ec2.Instance.

In looking at the boto3 documentation for EC2:

  • The ec2.Instance object includes an attribute called block_device_mappings (note that it is plural)
  • That attribute is a list that contains entries for DeviceName and an Ebs dictionary
  • The Ebs dictionary contains an entry called Status

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:

each_inst_in_reg.block_device_mappings[0].DeviceName
each_inst_in_reg.block_device_mappings[0].Ebs['Status']

To loop through each volume, you could use:

for volume in each_inst_in_reg.block_device_mappings:
  name = volume.DeviceName
  status = volume.Ebs['Status']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文