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

Created on 20 Sep 2013  ·  46Comments  ·  Source: aws/aws-cli

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"
}

documentation

Most helpful comment

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

All 46 comments

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

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).

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

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)

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

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

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.

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

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.

@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 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

Have you tried:

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

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

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

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

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.

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?

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

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

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.

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

Alright, this wasn't obvious, from the above, so hopefully it'll help somebody. This lists in a nice table format some useful fields including the Name tag (if you use that).

aws ec2 describe-instances --query 'Reservations[].Instances[].[ InstanceId,[Tags[?Key==Name].Value][0][0],State.Name,InstanceType,Placement.AvailabilityZone ]' --output table

$ aws ec2 describe-instances --filter Name=tag:Role,Values=cache for those who stumble here

The help is still super confusing about the tag filter. It says:

o tag :key =*value* - The key/value combination of a tag assigned to the resource.

which seems to suggest that you can do:

--filters Name=tag:SomeKey=SomeValue

which of course is not correct.

If anyone has a hard time figuring it out how to sort tags, following command works for me:
aws ec2 describe-instances --output json --query "Reservations[*].Instances[*].{ID:InstanceId,Type:InstanceType,Name:Tags[?Key=='Name'].Value}"

If you need to include tags with a : in them you need to quote, i.e.
aws ec2 describe-instances --region us-west-2 --filters Name=tag:"aws:autoscaling:groupName",Values=yourautoscalegroupname

I too was stymied by the impenetrable and misleading documentation on this.

I was trying to figure out how to get the IP address of an instance based on a tag (in this case, "Name"). I tried a few different things with --query and --filters, but it seems that getting from one nested parameter to another is complicated, or maybe not possible just using one or the other approach. Combining --query and --filters did the trick:

aws ec2 describe-instances --filters Name=tag:Name,Values="Instance Name" --query 'Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddresses[].PrivateIpAddress' --output text

This comes up at the top when I search for ec2.instances.filter(), to filter instances programmatically.


Here's how you would achieve this:
(Find all instances that have tag Name and the tag value starts with api-)

import boto3

ec2 = boto3.resource('ec2')

for instance in ec2.instances.filter(Filters=[{'Name': 'tag:Name', 'Values': ['api-*']}]):
    print instance

@adeelx for Python sure but the issue is with the bash awscli usage

Below command works for me

aws --version
aws-cli/1.11.81 Python/3.4.3 Linux/3.13.0-74-generic botocore/1.5.44
aws ec2 describe-instances --filters Name=tag:App,Values="value" --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,InstanceType,Platform,Tags.Value,State.Code,Tags.Values]' --output table

I wanted to filter my instances to those that had a specific prefix in the Name tag, and then ensure that in the output table the full Name was listed, the only way to achieve that seems to be to use a join() to flatten the list into a string:

aws ec2 describe-instances --filters "Name=tag:Name,Values=talis*" --query 'Reservations[].Instances[].[InstanceId,PublicDnsName,State.Name,InstanceType,join(`,`,Tags[?Key==`Name`].Value)]' --output table

I had similar issues and it was quite a struggle to get to this:
aws ec2 describe-tags --region ${region} --filter "Name=tag:Name,Values=*Rally" "Name=resource-id,Values=$instanceID"

5 years later, and that same terrible example is still in the documentation, and a better example still doesn't exist.

My Google search for how to use filters with the describe_* API calls has led me here, and I can't help but shake my head after reading @tolidano 's last comment.

In case you need to filter by the tag key only, you can do something like

ec2-describe-tags --filter "resource-type=instance" --filter "key=hostname" --filter "resource-id=$(ec2metadata --instance-id)" | awk '{print $5}'

Or simply use the --filter "key=<KEY>"

I'm looking to get ec2 platform type i.e. (Windows or Linux) using the lambda function filters. In case if anyone has done it could you please share the same

'Windows' is returned by the "Platform" field for Windows instances. None is returned for Linux instances. The query at the top of this thread still works today.

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

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

if need exactly instance name
aws ec2 describe-instances --filter Name=tag:Name,Values=devschemareg01 --output text | sort | grep Name | awk '{print $3}'

Sharing what i have used:
```instance-id = curl --silent http://169.254.169.254/latest/meta-data/instance-id 2>&1
aws ec2 describe-tags --region us-west-1 --filters Name=resource-id,Values=[instance-id] Name=tag:aws:cloudformation:stack-name,Values=[“my_stack_name"]

or

aws ec2 describe-tags --region us-west-1 --filters Name=resource-id,Values=[instance-id] Name=key,Values=aws:cloudformation:stack-name

aws ec2 describe-instances --filters Name=tag:<YOUR_TAG>,Values="<YOUR_VALUE>" --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,InstanceType,InstanceLifecycle,Tags[?Key==`<YOUR_TAG`]|[0].Value]'  --output table

I've used below filters to get the list of instances with public IP and their respective name usign --filter options and it works fine.

aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" "Name=tag:OS,Values=AMAZON/LINUX2" --query "Reservations[].Instances[].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" --output table

I have used below query to get AZ, InstanceType, Name, PrivateIpAddress, PublicIpAddress and state of the running ec2 instances:

aws ec2 describe-instances --query 'Reservations[].Instances[].{State:State.Name,PublicIpAddress:PublicIpAddress,InstanceType:InstanceType,PrivateIP:PrivateIpAddress,AZ:Placement.AvailabilityZone,Name:Tags[?Key==Name]|[0].Value}' --output table | grep -i "ignoredCaseNameOfEc2Instance"

To filter the details by name, i am using grep with -i option to have case-insensitive search instead of filters by name..

P.S. : While writing command, i was mostly struggling with the pipe operator highlighted before [0].Value in tag filter of name. (might be helpful for someone struggling with the command)

@vaibhavg2510 while that approach works, and kudos, it does "waste" API commands. In a busy account, this makes you more subject to throttling, long running processes due to exponential backoff/retry, etc.

Bump. The --filters "Name = tag:Name, Values = ..." syntax is still not obvious from the documentation. I literally googled for 15 minutes until I found this issue which finally showed how to do this.

I spoke with the EC2 writers about this. They told me that they would work on an example to clarify this. In the interim, the syntax is documented in the EC2 User Guide here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_CLI

I hope this helps!

I'm the latest victim I think that had to come here to get my answer. Are the plans to consolidate the docs still existing?

Even a link from the API page to the above comments link (or maybe at this stage this repo) would be beneficial.

Was this page helpful?
0 / 5 - 0 ratings