Python 使用 boto 从 AWS 实例获取标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19516678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:56:35  来源:igfitidea点击:

Obtaining tags from AWS instances with boto

pythonamazon-web-servicesbotoinstances

提问by rodolk

I'm trying to obtain tags from instances in my AWS account using Python's boto library.

我正在尝试使用 Python 的 boto 库从我的 AWS 账户中的实例获取标签。

While this snippet works correctly bringing all tags:

虽然此代码段可以正确加载所有标签:

    tags = e.get_all_tags()
    for tag in tags:
        print tag.name, tag.value

(e is an EC2 connection)

(e 是 EC2 连接)

When I request tags from individual instances,

当我从单个实例请求标签时,

    print vm.__dict__['tags']

or

或者

    print vm.tags

I'm getting an empty list (vm is actually an instance class).

我得到一个空列表(vm 实际上是一个实例类)。

The following code:

以下代码:

    vm.__dict__['tags']['Name']

of course results in:

当然结果是:

KeyError: 'Name'

My code was working until yesterday and suddenly I'm not able to get the tags from an instance.

我的代码一直工作到昨天,突然间我无法从实例中获取标签。

Does anybody know whether there is a problem with AWS API?

有人知道AWS API是否有问题吗?

采纳答案by andpei

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)

will print:

将打印:

i-4e444444 [stopped]
Amazon Linux (i-4e333333) [running]

回答by garnaat

Try something like this:

尝试这样的事情:

import boto.ec2

conn = boto.ec2.connect_to_region('us-west-2')
# Find a specific instance, returns a list of Reservation objects
reservations = conn.get_all_instances(instance_ids=['i-xxxxxxxx'])
# Find the Instance object inside the reservation
instance = reservations[0].instances[0]
print(instance.tags)

You should see all tags associated with instance i-xxxxxxxxprinted out.

您应该会看到i-xxxxxxxx打印出的与实例关联的所有标签。

回答by rodolk

It turned out to be an error in my code. I did not consider the case of having one instance without the tag 'Name'.

结果证明是我的代码中的错误。我没有考虑有一个没有标签“名称”的实例的情况。

There was one instance without the tag "Name" and my code was trying to get this tag from every instance.

有一个没有标签“Name”的实例,我的代码试图从每个实例中获取这个标签。

When I ran this piece of code in an instance without the tag 'Name',

当我在没有标签“名称”的实例中运行这段代码时,

vm.__dict__['tags']['Name']

I got: KeyError: 'Name'. vm is a AWS instance. With the instances that actually had this tag set, I didn't have any problem.

我得到: KeyError: 'Name'。vm 是一个 AWS 实例。对于实际设置了此标记的实例,我没有任何问题。

Thank you for your help and sorry for asking when it was only my own mistake.

谢谢你的帮助,很抱歉问这只是我自己的错误。

回答by nu everest

For boto3 you will need to do this.

对于 boto3,您需要这样做。

import boto3
ec2 = boto3.resource('ec2')
vpc = ec2.Vpc('<your vpc id goes here>')
instance_iterator = vpc.instances.all()

for instance in instance_iterator:
    for tag in instance.tags:
        print('Found instance id: ' + instance.id + '\ntag: ' + tag)