使用boto从AWS实例获取标签 [英] Obtaining tags from AWS instances with boto
本文介绍了使用boto从AWS实例获取标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Python的boto库从我的AWS账户中的实例获取标签.
I'm trying to obtain tags from instances in my AWS account using Python's boto library.
虽然此代码段可以正常运行,但可以带上所有标签:
While this snippet works correctly bringing all tags:
tags = e.get_all_tags()
for tag in tags:
print tag.name, tag.value
(e是EC2连接)
当我从各个实例中请求标签时,
When I request tags from individual instances,
print vm.__dict__['tags']
或
print vm.tags
我得到一个空列表(vm实际上是一个实例类).
I'm getting an empty list (vm is actually an instance class).
以下代码:
vm.__dict__['tags']['Name']
当然会导致:
KeyError: 'Name'
我的代码一直工作到昨天,突然之间我无法从实例中获取标签.
My code was working until yesterday and suddenly I'm not able to get the tags from an instance.
有人知道AWS API是否有问题吗?
Does anybody know whether there is a problem with AWS API?
推荐答案
访问它之前,必须确保'Name'标记存在.试试这个:
You have to be sure that the 'Name' tag exists before accessing it. Try this:
import boto.ec2
conn=boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
for res in reservations:
for inst in res.instances:
if 'Name' in inst.tags:
print "%s (%s) [%s]" % (inst.tags['Name'], inst.id, inst.state)
else:
print "%s [%s]" % (inst.id, inst.state)
将打印:
i-4e444444 [stopped]
Amazon Linux (i-4e333333) [running]
这篇关于使用boto从AWS实例获取标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文