aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

aws ec2 describe-instances --filters tag: seems to be not working (or docs is missing/misleading)

Open zasran opened this issue 13 years ago • 48 comments

Trying to use aws ec2 describe-instance (version 1.1.0) with tag: filter which is mentioned in docs at http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html but it doesn't seem to be working.

Quote from documentation: "tag: --filters the response based on a specific tag/value combination."

Tried several formats (just guessing, trying format used by http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeInstances.html) but no luck.

This does not complain but returns no instances:

aws ec2 describe-instances --filters Name=tag:,Values=Name=ADS-prod-ads

This one fails:

aws ec2 describe-instances --filters tag:Name=ADS-prod-ads Unknown key 'tag:Name' for parameter --filters, valid choices are: Values, Name

Just for reference, using tag-value works (just to show that tag Name=ADS-prod-ads exists):

aws ec2 describe-instances --filters Name=tag-value,Values=ADS-prod-ads | jq '.Reservations[].Instances[].Tags[]' { "Key": "Name", "Value": "ADS-prod-ads" }

zasran avatar Sep 20 '13 01:09 zasran

To filter on all instances that have a tag with a name or key of Name AND a value of ADS-prod-ads, you would do this:

aws ec2 describe-instances --filter Name=tag:Name,Values=ADS-prod-ads

garnaat avatar Sep 20 '13 11:09 garnaat

Thanks, that works. It would be great if the example was in documentation (think it's not obvious, it is slightly different from other filters).

Also the documentation for tag-key says: "If you want to list only resources where Purpose is X, see the key filter later in this table." As far as I can tell it should say "see the tag: filter".

Documentation in the above refers to both "aws ec2 describe-instances help" and http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html which seem to be the same (i.e. generated from the same source).

zasran avatar Sep 20 '13 16:09 zasran

I agree that an example would be very helpful. I will tag this as a documentation issue. Thanks.

garnaat avatar Sep 20 '13 21:09 garnaat

Took a while to find this considering how useful, yes, would be nice to have one example in aws ec2 describe-instances doc (http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html)

leddy-c3 avatar Dec 06 '13 23:12 leddy-c3

Yes, this now works. The example supplied by AWS Docs is a bit confusing but to clarify. I have a tag called 'Resource' and each instance is assigned a value either 'test', 'stage' or 'live'. To find all instances (running or stopped) with the resource tag of 'test' this is the syntax:

aws ec2 describe-instances --filters Name=tag:resource,Values=test --profile test-aws

wml2013 avatar Dec 18 '13 16:12 wml2013

Im having the same problem with --query. I just want the Name of the instance in the report and its coming out black. I'm using the following query

describe-instances --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,InstanceType,Platform,Tags.Value,State.Code,Tags.Values]' --output table

derrickfn avatar Feb 27 '14 14:02 derrickfn

Are you using the latest version of was cli ?

It works for me

aws ec2 describe-instances --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,InstanceType,Platform,Tags.Value,State.Code,Tags.Values]' --output table
---------------------------------------------------------------------------------------
|                                  DescribeInstances                                  |
+------------+----------+-------------+------------+----------+-------+------+--------+
|  eu-west-1c|  stopped |  i-60xxxx2a |  t1.micro  |  None    |  None |  80  |  None  |
|  eu-west-1a|  stopped |  i-6fxxxx720 |  m1.medium |  windows |  None |  80  |  None  |
|  eu-west-1c|  running |  i-1axxxx56 |  t1.micro  |  None    |  None |  16  |  None  |
+------------+----------+-------------+------------+----------+-------+------+————+

-- Seb

On 27 Feb 2014, at 15:16, derrickfn [email protected] wrote:

Im having the same problem with --query. I just want the Name of the instance in the report and its coming out black. I'm using the following query

describe-instances --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,InstanceType,Platform,Tags.Value,State.Code,Tags.Values]' --output table

— Reply to this email directly or view it on GitHub.

sebsto avatar Feb 27 '14 14:02 sebsto

Greetings Sebsto,

The command is working, but it is not returning the actual tag values. Its giving me None(NULL), just like it did you. For the Name element we plug in names for all of your servers to make them easier to identify. We also Tag the environment they they are part of.

Thanks in advance for the help! Derrick

derrickfn avatar Feb 27 '14 14:02 derrickfn

Sorry - I answered too fast

For Platform : this is expected as only Windows instances have a Platform attribute in the JSON description For the rest, looks like there is indeed an issue

OK: aws ec2 describe-instances --query Reservations[].Instances[].Tags.Key

Not OK: aws ec2 describe-instances --query Reservations[].Instances[].[Tags.Key]

-- Seb

On 27 Feb 2014, at 15:38, derrickfn [email protected] wrote:

Greetings Sebsto,

The command is working, but it is not returning the actual tag values. Its giving me None(NULL), just like it did you. For the Name element we plug in names for all of your servers to make them easier to identify. We also Tag the environment they they are part of.

Thanks in advance for the help! Derrick

— Reply to this email directly or view it on GitHub.

sebsto avatar Feb 27 '14 15:02 sebsto

@sebasto The issue with the above query is that Tags is a list, so the expression Tags.Key will return null. If you wanted all the Key names you would say Tags[].Key.

However, in the latest CLI version (1.3.0) just released, there's a more direct way to filter by specific names. For example, if you wanted the name of all your instances in a list you can now say:

aws ec2 describe-instances --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value[]'

[
  "name1",
  "myinstance2",
  ...
  "devinstance"
]

jamesls avatar Feb 28 '14 02:02 jamesls

@jamesls How would one go about expanding this query to include more results in the output? For example I am using the following to list things like InstanceID, AZ, etc aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Placement.AvailabilityZone, InstanceType, Platform, State.Name, PublicDnsName]' --output text |grep running But when I add "Tags[?Key==Name].Value[]" to my query I cannot get the Name Tag to return. I am only getting "None" for that field. Here's an example of my aws command: aws ec2 describe-instances --query 'Reservations[].Instances[].["Tags[?Key==Name].Value[]", InstanceId, Placement.AvailabilityZone, InstanceType, Platform, State.Name, PublicDnsName]' --output text |grep running

jbunker avatar Mar 13 '14 17:03 jbunker

Have you tried:

Reservations[].Instances[].["Tags[*][?Key==Name].Value[]

garnaat avatar Mar 13 '14 18:03 garnaat

Sorry, that's not it. How about this?

aws ec2 describe-instances --query 'Reservations[].Instances[].Tags[*][?Key==`Name`].Value'

garnaat avatar Mar 13 '14 18:03 garnaat

Yes, that works but only gives me the name tag. I'm trying to get that and other information like InstanceID, AZ, etc.

jbunker avatar Mar 13 '14 18:03 jbunker

Well, this works for me:

aws ec2 describe-instances --query 'Reservations[].Instances[].{id: InstanceId, tagvalue: Tags[*][?Key==`Name`].Value}'

You could expand this to get other items besides just InstanceId.

garnaat avatar Mar 13 '14 20:03 garnaat

Sorry to bump like this, but I have an issue with a query:

aws ec2 describe-instances --query Reservations[].Instances[].SecurityGroups[?GroupName==default].GroupId

I use the above to try to extract the groupid of a certain group (name=default in this case). But nothing is returned. Any ideas?

cristifalcas avatar Jul 04 '14 15:07 cristifalcas

I think I am stuck somewhere, when i run this I get None everywhere

aws --region us-east-1 ec2 describe-instances --query 'Reservations[].Instances[].Tags[?Key==Name].Value[].[Placement.AvailabilityZone, State.Name, InstanceId,LaunchTime, ImageId]' --output table

asvarshn avatar Dec 04 '14 02:12 asvarshn

This is because .Value[] just returns the value of your name tags. All the other attributes requested after that does not exist in this output

sebsto avatar Dec 04 '14 12:12 sebsto

The filters have a awful syntax. I can google with you, try to figure out.

On 12/03/2014 06:14 PM, asvarshn wrote:

I think I am stuck somewhere, when i run this I get None everywhere

aws --region us-east-1 ec2 describe-instances --query 'Reservations[].Instances[].Tags[?Key==|Name|].Value[].[Placement.AvailabilityZone, State.Name, InstanceId,LaunchTime, ImageId]'

— Reply to this email directly or view it on GitHub https://github.com/aws/aws-cli/issues/368#issuecomment-65527379.

leddy-c3 avatar Dec 04 '14 16:12 leddy-c3

I have got this working for me..

aws --region eu-west-1 ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==Name].Value | [0], InstanceId, Placement.AvailabilityZone, InstanceType, LaunchTime, State.Name, PublicDnsName]' --output table

asvarshn avatar Dec 04 '14 23:12 asvarshn