I'm working from the instance id. I can get the tags, but I can't figure out how filter on both the instance id and the tag "Name" or index in and return the value of the tag called "Name"
I ended up grabbing all tags and iterating over them till I find the one I want, which can't possibly be right.
tags = conn.get_all_tags({'resource-id': instance_id})
for tag in tags:
if 'Name' in tag.name:
name = tag.value
你确实有更好的方法:
conn.get_all_tags(filters={'tag-key': 'Name', 'resource-id': instance_id})
I think the way you're doing it is fine. You could always wrap it in a function call as an abstraction:
def get_instance_tag(all_tags, tag_name):
for tag in all_tags:
if tag_name == tag.name:
return tag.value
return None
name = get_instance_tag(conn.get_all_tags({'resource-id': instance_id}), 'Name')
Note that if tag_name == tag.name:
is more accurate than if tag_name in tag.name:
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.