AWS
Amazon Web Services
Cloud Management
Instance Tagging
Resource Optimization

Finding all Amazon AWS Instances That Do Not Have a Certain Tag

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In AWS, tags are often used for ownership, cost allocation, automation, and access control. That makes missing tags a real operational problem, not just a cosmetic one. If an EC2 instance is missing a required tag such as Environment or Owner, it can fall out of cost reports, patch rules, or cleanup workflows.

The tricky part is that EC2 filtering is better at finding resources that do have a tag than at expressing "show me everything that does not have this tag." In practice, the cleanest solution is usually to list the relevant instances and filter client-side.

A Reliable Boto3 Approach

The following script paginates through EC2 instances and prints the ones that do not contain a required tag key:

python
1import boto3
2
3
4REQUIRED_TAG = "Environment"
5
6
7def has_tag(instance, tag_key):
8    tags = instance.get("Tags", [])
9    return any(tag["Key"] == tag_key for tag in tags)
10
11
12def find_instances_missing_tag(region_name):
13    ec2 = boto3.client("ec2", region_name=region_name)
14    paginator = ec2.get_paginator("describe_instances")
15
16    for page in paginator.paginate():
17        for reservation in page["Reservations"]:
18            for instance in reservation["Instances"]:
19                if not has_tag(instance, REQUIRED_TAG):
20                    instance_id = instance["InstanceId"]
21                    state = instance["State"]["Name"]
22                    print(f"{instance_id}\t{state}\tmissing {REQUIRED_TAG}")
23
24
25find_instances_missing_tag("us-east-1")

This approach is easy to trust because it handles pagination and checks the actual Tags array for each instance. It also works whether the instance has no tags at all or simply lacks the specific required key.

Narrow the Search Scope First

In large accounts, you usually do not want to scan every region and every instance indiscriminately. Narrow the scope by region, account, or instance state first.

For example, if you only care about running instances:

python
1for page in paginator.paginate(
2    Filters=[{"Name": "instance-state-name", "Values": ["running"]}]
3):
4    for reservation in page["Reservations"]:
5        for instance in reservation["Instances"]:
6            if not has_tag(instance, REQUIRED_TAG):
7                print(instance["InstanceId"])

Filtering this way reduces API volume while still preserving the missing-tag check in your own code.

Include More Context in the Report

A missing-tag report is much more useful when it includes enough metadata for remediation. For example, you may want the instance name, account, region, or launch time.

python
1def get_name_tag(instance):
2    for tag in instance.get("Tags", []):
3        if tag["Key"] == "Name":
4            return tag["Value"]
5    return "(no Name tag)"
6
7
8print(
9    f"{instance['InstanceId']}\t"
10    f"{get_name_tag(instance)}\t"
11    f"{instance['State']['Name']}\t"
12    f"missing {REQUIRED_TAG}"
13)

That extra context often matters more than the raw instance ID because the person fixing the tags usually wants to know what system they are touching.

Alternative Approaches

You can also use AWS Config rules or organizational governance tools when tag compliance needs to be continuous rather than occasional. Those services are better for policy enforcement. The Boto3 script is the right fit when you want an ad hoc audit, a scheduled report, or a remediation script inside an existing automation job.

For one-off CLI checks, developers sometimes use aws ec2 describe-instances with a JMESPath query. That can work, but client-side Python is usually easier to read and easier to extend once the logic grows beyond a simple filter.

Common Pitfalls

  • Assuming EC2 has a simple server-side "missing tag" filter for every use case. It is often easier and clearer to filter in client code.
  • Forgetting pagination. Large accounts can silently produce incomplete results if you inspect only the first page.
  • Scanning only one region and assuming the account is fully covered.
  • Treating missing Tags as an error instead of a valid case that should count as non-compliant.
  • Producing reports with only instance IDs and no context, which slows down remediation.

Summary

  • The safest way to find EC2 instances missing a tag is to list instances and filter client-side.
  • Use a paginator so large accounts do not produce incomplete audits.
  • Narrow the scan with state or region filters when appropriate.
  • Include contextual fields such as Name and state in the output.
  • Use AWS Config or governance tooling when you need ongoing compliance, not just a one-time report.

Course illustration
Course illustration

All Rights Reserved.