问题
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