如果实例没有标签,则显示实例 ID "Name"

Display instance Id's if the instance has no tag "Name"

我有一项要求显示没有名为 Name 的标签的实例 ID。

我尝试了以下 cli 命令来查找但对我不起作用。

aws ec2 describe-tags --filters Name="",Values=""

aws ec2 describe-tags

无法搜索不存在的标签。

您需要检索所有实例的列表,然后排除没有标签的实例。

您可以在 Python 中这样做:

import boto3

ec2_resource = boto3.resource('ec2')

for instance in ec2_resource.instances.all():
    if 'Name' not in [tag['Key'] for tag in instance.tags]:
        print(instance.id)