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:
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:
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.
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
Tagsas 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
Nameand state in the output. - Use AWS Config or governance tooling when you need ongoing compliance, not just a one-time report.

