Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to use the Amazon AWS Command Line Tools to find all instances that do not have a specified tag.

Finding all instances WITH a tag is simple enough, e.g.

ec2-describe-instances --filter "tag-key=Name" 

But how would I invert that filter to return only the instances that have no tag "Name"?

like image 515
A. Duff Avatar asked Sep 17 '13 19:09

A. Duff


People also ask

Where can I find AWS resources without tags?

To find resources that have an empty value for a tag, choose (empty value) when your cursor is in the tag value box. Before you can find resources with the specified tags, they must have been applied to at least one resource of the specified type in the current AWS Region.

How do I see all tags in AWS?

On the Manage tags page, in Edit tags of selected resources, view the tags on the resource that you selected. Although your original query returned more resources, note that you are changing tags only on the resources that you selected in step 1. Change, add, or delete tag values.

How do I get a list of EC2 instances?

Go to the instances section and click on "instances". It will show you all the running instances in the select region.

Can all AWS resources be tagged?

When you tag public or shared resources, the tags you assign are available only to your AWS account; no other AWS account will have access to those tags. For tag-based access control to shared resources, each AWS account must assign its own set of tags to control access to the resource. You can't tag all resources.


1 Answers

You can do that with jmespath (the engine that drives the --query parameter) despite what others say:

aws ec2 describe-instances \   --query 'Reservations[].Instances[?!not_null(Tags[?Key == `Name`].Value)] | []' 

Source: Using Amazon Web Services Command Line Interface (AWS CLI) to Find Instances without a 'Name' Tag.

like image 85
Nate Fox Avatar answered Sep 21 '22 05:09

Nate Fox