Boto 脚本,用于提取没有特定标签(名称)的 ec2 实例



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

名称的价值可以是任何东西。我们只需要尚未设置名称的实例。

我已经尝试过:这是正确的吗?它正在返回一些实例,但也会遇到此错误:

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

什么将代替Foocos我必须列出没有与之关联的名称标签的实例。

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对象!虽然只是在循环中使用对象,因为该对象的迭代器定义了

最新更新