Boto脚本用于获取没有特定标记(Name)的ec2实例

时间:2017-09-26 11:21:18

标签: amazon-web-services boto3

有人可以帮助我使用boto脚本,该脚本列出了没有与之关联的特定标记名称的ec2实例。

Name的值可以是任何值。我们只需要尚未设置Name的实例。

我试过这个:这是对的吗?它返回了一些实例,但也收到了这个错误:

Traceback (most recent call last):
  File "try6.py", line 7, in <module>
    if 'Foo' not in [t['Key'] for t in i.tags]:
TypeError: 'NoneType' object is not iterable




import boto3

instances = [i for i in boto3.resource('ec2', region_name='us-east-1').instances.all()]

# Print instance_id of instances that do not have a Tag of Key='Foo'
for i in instances:
    if 'Foo' not in [t['Key'] for t in i.tags]:
        print i.instance_id
什么将代替Foo 我必须列出没有与之关联的名称标签的实例 〜

1 个答案:

答案 0 :(得分:0)

instances=boto3.resource('ec2', region_name='eu-west-1').instances.all()

for i in instances:
    if 'Foo' not in [t['Key'] for t in i.tags]:
        print i.instance_id

我认为这适用于你的例子没有.instances.all()返回boto3.resources.collection.ec2.instancesCollection

所以[]构造会生成一个boto3.resources.collection.ec2.instancesCollection对象的列表!而只是在循环中使用对象,因为对象具有定义的迭代器