2017-09-26 1 views
-1

特定のタグ名を持たないec2インスタンスをリストするbotoスクリプトを手伝ってもらえますか?特定のタグ(名前)を持たないec2インスタンスをフェッチするためのBotoスクリプト

名前の値は何でもかまいません。名前が設定されていないインスタンスが必要です。

私はこれを試しました:これは正しいですか?それはいくつかのインスタンスを返すだけでなく、このエラーを取得している:私はそれらに関連付けられた名前のタグを持っていないインスタンスを一覧表示する必要がcosをFooの の代わりに行く何

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

awt-cliと同様にboto、boto3でタグ付けします。あなたが使っているもの、それについてどれだけ読んだり、何を試してみたか、よく整形された質問をしてください – 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オブジェクト!ループ内のオブジェクトを使用するだけで、オブジェクトの反復子が定義されているように動作します。

関連する問題