Get volume information associated with Instance

馋奶兔 提交于 2019-12-25 07:39:40

问题


I'm trying to retrieve all the volumes associated with an instance.

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

However, it's not working as intended.


回答1:


You can add filters in get_all_instances method

like this:

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


来源:https://stackoverflow.com/questions/41318892/get-volume-information-associated-with-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!