如何使用 Boto3 查找没有标签的实例
How to find instances that DONT have a tag using Boto3
我正在尝试查找没有特定标签的实例。
例如,我想要所有没有 Foo 标签的实例。
我还想要 Foo 值不等于 Bar 的实例。
这就是我现在正在做的事情:
import boto3
def aws_get_instances_by_name(name):
"""Get EC2 instances by name"""
ec2 = boto3.resource('ec2')
instance_iterator = ec2.instances.filter(
Filters=[
{
'Name': 'tag:Name',
'Values': [
name,
]
},
{
'Name': 'tag:Foo',
'Values': [
]
},
]
)
return instance_iterator
这没有返回任何内容。
正确的方法是什么?
下面是一些代码,可以在 没有 特定标签的情况下显示 instance_id
:
import boto3
instances = [i for i in boto3.resource('ec2', region_name='ap-southeast-2').instances.all()]
# Print instance_id of instances that do not have a Tag of Key='Foo'
for i in instances:
if i.tags is not None and 'Foo' not in [t['Key'] for t in i.tags]:
print i.instance_id
我正在尝试查找没有特定标签的实例。
例如,我想要所有没有 Foo 标签的实例。 我还想要 Foo 值不等于 Bar 的实例。
这就是我现在正在做的事情:
import boto3
def aws_get_instances_by_name(name):
"""Get EC2 instances by name"""
ec2 = boto3.resource('ec2')
instance_iterator = ec2.instances.filter(
Filters=[
{
'Name': 'tag:Name',
'Values': [
name,
]
},
{
'Name': 'tag:Foo',
'Values': [
]
},
]
)
return instance_iterator
这没有返回任何内容。
正确的方法是什么?
下面是一些代码,可以在 没有 特定标签的情况下显示 instance_id
:
import boto3
instances = [i for i in boto3.resource('ec2', region_name='ap-southeast-2').instances.all()]
# Print instance_id of instances that do not have a Tag of Key='Foo'
for i in instances:
if i.tags is not None and 'Foo' not in [t['Key'] for t in i.tags]:
print i.instance_id