简体   繁体   English

无法使用 Boto3 打印实例 ID

[英]Unable to Print instance id using Boto3

I was trying to print the InstanceId followed by the VolumeID and state.我试图打印 InstanceId 后跟 VolumeID 和 state。 I am able to print the 'VolumeID' and 'state' but I am getting an 'KeyError' while trying to print the 'InstanceID'.我可以打印“VolumeID”和“state”,但在尝试打印“InstanceID”时出现“KeyError”。 Not sure on what I am doing wrong here.不确定我在这里做错了什么。

ec2 = boto3.client('ec2')
volumes = ec2.describe_volumes()

for i in volumes['Volumes']:
    print(i['InstanceId'])
    print(i['VolumeId'])
    print(i['State']) 

The InstanceId for the volume is on its attachments property , which is a list you can iterate through:卷的InstanceId位于它的attachments属性上,这是一个您可以遍历的list

ec2 = boto3.client('ec2')
volumes = ec2.describe_volumes()

for i in volumes['Volumes']:
    for a in i.attachments:
        print(a['InstanceId'])
    print(i['VolumeId'])
    print(i['State']) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM