anakaine
anakaine

Reputation: 1248

Filtering by tags not working when using aws ec2 describe-instances from command line (cli)

I'm currently attempting to write an aws ec2 query from the command line (in AWS Linux, not that it should matter). I am trying to set a filter that matches both of the following:

  1. Shows those instances that are in the off state (code 80), and;
  2. Shows those instances who have a tag "ShortPurpose" whose value is "Fleet".

What is actually happening is that all instances in the off state are being returned, regardless of if they have the tag "ShortPurpose":"Fleet" set.

My instances are set up as so:

+-------------+--------------+------------------------+--+
| Instance ID |     Tag      |       Tag Value        |  |
+-------------+--------------+------------------------+--+
| i-09876     | ShortPurpose | Fleet                  |  |
|             | Organisation | UmbrellaCorp           |  |
|             | Name         | cloud-01               |  |
|             | Owner        | ORG-UMBR-ELLA          |  |
|             | Purpose      | Cloud processing fleet |  |
+-------------+--------------+------------------------+--+
|             |              |                        |  |
| i-12345     |  (no tags)   |                        |  |
|             |              |                        |  |
+-------------+--------------+------------------------+--+

The command I am using is:

aws ec2 describe-instances --query "Reservations[*].Instances[*].InstanceId" --filters "Name=tag:ShortPurpose,Values=Fleet,Name=instance-state-code,Values=80"

The results are the standard array style response. The instance state is successfully filtered upon, but not the tags.

Upvotes: 6

Views: 8942

Answers (2)

Marcin
Marcin

Reputation: 238061

I tried to verify your command and it produces errors as you wrote it:

Error parsing parameter '--filters': Second instance of key "Name" encountered for input:
Name=tag:ShortPurpose,Values=Fleet,Name=instance-state-code,Values=80
                                   ^
This is often because there is a preceeding "," instead of a space.

However, I was able to successful use it on my sandbox instances as follows:

aws ec2 describe-instances \
     --query "Reservations[*].Instances[*].InstanceId" \
     --filters Name=tag:ShortPurpose,Values=Fleet Name=instance-state-code,Values=80

Upvotes: 5

anakaine
anakaine

Reputation: 1248

I have discovered in one example in the AWS doumentation that I had the wrong query format. The correct query is:

aws ec2 describe-instances --query "Reservations[*].Instances[*].InstanceId" --filters "Name=tag-value,Values=Fleet" "Name=instance-state-code,Values=80"

Note that I'm ignoring the ShortPurpose tag and instead hunting directly for the value, which may exist in any tag.

Upvotes: 4

Related Questions