DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • File Upload Security and Malware Protection
  • Chaining API Requests With API Gateway

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • File Upload Security and Malware Protection
  • Chaining API Requests With API Gateway
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Find EC2 Instances That Are Missing Tags

How to Find EC2 Instances That Are Missing Tags

jq is a lightweight and flexible command-line JSON processor. Yeah, I know..another third-party tool. However, it is well worth it.

Chris Short user avatar by
Chris Short
·
Jan. 30, 17 · Opinion
Like (3)
Save
Tweet
Share
7.68K Views

Join the DZone community and get the full member experience.

Join For Free

The team here was trying to align AWS resources for billing by using tags. Seems easy enough. However, we were having issues trying to figure out which EC2 instances were not tagged. We also wanted to find EC2 instances that are missing a ROLE tag. We searched high and low and found that several people needed a solution. We tried to cobble something together with the AWS CLI and then Ansible. We could not come up with a viable solution. So, we did what any could DevOps team should do: We opened a ticket with AWS Support.

The response from AWS came quickly and succinctly. Here is what we came up with after their input.

Display instances that have no tags:

aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId, Tags]" --output text | grep None | awk '{print $1}'

Display instances that are missing a specific tag (defined by key):

aws ec2 describe-instances --query "Reservations[].Instances[].{ID: InstanceId, Tag: Tags[].Key}" --output text | grep -v ROLE

Voila! Problem solved, right? Not quite. We noticed that the output formats are very different based on region.

In eu-central-1, we are seeing instances in this format:

i-cd5ade70  
TAG    Name  
TAG    OWNER  
TAG    SYSTEM  

Inus-east-1, we are seeing instances in this format:

i-0d879e60cb5053af3 ['aws:autoscaling:groupName', 'Name']

We are thinking that the API versions in each region were different but aren't positive. How did we fix this? We needed a consistent format and we landed on JSON. However, now we have a screen full of JSON that while consistently formatted isn't easily editable.

Enter jq. jq is a lightweight and flexible command-line JSON processor.

Yeah, I know..another third-party tool. However, when all you have to do is pipe the AWS command to jq -c '.[]' to get a grep -v-able output, it is well worth it.

aws ec2 describe-instances --query "Reservations[].Instances[].{ID: InstanceId, Tag: Tags[].Key}" --output json | jq -c '.[]' | grep -v ROLE  

jq is available from Brew, Epel, and the jq website.

AWS

Published at DZone with permission of Chris Short. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • File Upload Security and Malware Protection
  • Chaining API Requests With API Gateway

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: