如何使用Boto3查找DONT有标签的实例

Ayu*_*rma 4 python amazon-ec2 boto3

我正在尝试找到DONT有特定标签的实例.

例如,我想要所有没有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
Run Code Online (Sandbox Code Playgroud)

这没有任何回报.

什么是正确的方法?

Joh*_*ein 5

以下是一些代码,它们将显示没有特定标记的instance_idfor实例:

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 'Foo' not in [t['Key'] for t in i.tags]:
    print i.instance_id
Run Code Online (Sandbox Code Playgroud)