How can I get list of only running instances when using ec2-describe-tags
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ec2-describe-tags can filter tags by key, value, resource type, and resource ID, but it does not know whether an EC2 instance is running or stopped. Instance state belongs to the instance description API, not the tag API. So if you want "tags for running instances only," you need a two-step approach or, better yet, the modern AWS CLI command that returns both state and tags together.
Why ec2-describe-tags Cannot Do It Alone
The old EC2 API tools split instance metadata across multiple commands:
- instance state comes from instance descriptions
- tags come from the tag listing API
That means this requirement actually has two filters:
- find instances whose state is
running - show the tags for those instance IDs
With the legacy tools, ec2-describe-tags only handles the second part.
The Modern AWS CLI Way
If you are not forced to stay on the old EC2 API tools, the cleanest solution is to query describe-instances directly. That API already returns tags on each instance object, so you do not need a second command.
That command asks EC2 for instances in the running state and then projects each instance ID together with its tag list.
If you only want the Name tag:
This is usually the best answer today because it keeps state and tags in one response and avoids extra shell plumbing.
A Legacy Two-Step Pattern
If you specifically must use ec2-describe-tags, first gather running instance IDs and then query tags for those IDs.
Using the current AWS CLI as the first step:
This works because the loop performs the join that ec2-describe-tags cannot perform by itself.
If you are fully on the legacy EC2 API tools, the exact state query syntax may differ by package version, but the idea stays the same:
- list running instance IDs
- pass those IDs into the tags command
Filter by State and Tag at the Same Time
A common real requirement is not just "running instances" but "running instances with a particular tag." In that case, modern AWS CLI filtering is even more useful.
This avoids a separate tag lookup completely.
Format the Output Safely
If you plan to feed the results into scripts, prefer structured output over ad hoc parsing.

