获取与Instance关联的卷信息

时间:2016-12-25 05:26:49

标签: boto boto3

我正在尝试检索与实例关联的所有卷。

if volume.attachment_state() == 'attached':

        volumesinstance = ec2_connection.get_all_instances()

        ids = [z for k in volumesinstance for z in k.instances]

        for s in ids:

            try:
                tags = s.tags
                instance_name = tags["Name"]
                print (instance_name)
            except Exception as e:
                print e

但是,它没有按预期工作。

1 个答案:

答案 0 :(得分:0)

您可以在get_all_instances方法

中添加过滤器 像这样:

filter = {'block-device-mapping.volume-id': volume.id}
        volumesinstance = ec2_connection.get_all_instances(filters=filter)

ids = [z for k in volumesinstance for z in k.instances]

for s in ids:

   try:
       tags = s.tags
       instance_name = tags["Name"]
       print (instance_name)
   except Exception as e:
       print e