2017-09-26 55 views
-1

有人可以帮助我使用boto脚本,该脚本列出了没有特定标签名称的ec2实例。Boto脚本可以获取没有特定标签(名称)的ec2实例

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 

什么将取代美孚 的去因为我必须列出不具有与其相关联的名称标签的情况。

+1

您正在标记与boto,boto3以及aws-cli。选择你正在使用的内容,你已经阅读了多少内容,你尝试了什么;然后询问一个格式良好的问题 – hjpotter92

+0

使用AWS Config执行此操作可能更合适 – Vorsprung

回答

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对象!而只是在循环中使用对象的工作,因为对象有一个迭代器定义