Aws-cli: having trouble getting multiple filters to work

Created on 7 Jan 2014  Â·  13Comments  Â·  Source: aws/aws-cli

I want to describe all instances that match two tags. Perhaps I don't have the syntax right, but I can't find it documented anywhere.

aws ec2 describe-instances --filters "Name=tag:Name,Values=xxx,Name=tag:env,Values=dev"

gives me all instances by tag:env, and ignores tag:Name

documentation

Most helpful comment

Does it do what you want if you separate the two tags as distinct arguments to the filters option?

aws ec2 describe-instances --filters "Name=tag:Name,Values=xxx" "Name=tag:env,Values=dev"

I agree that it is a bit odd to pass multiple values to a single option in *nix commands, but it works for me.

All 13 comments

Does it do what you want if you separate the two tags as distinct arguments to the filters option?

aws ec2 describe-instances --filters "Name=tag:Name,Values=xxx" "Name=tag:env,Values=dev"

I agree that it is a bit odd to pass multiple values to a single option in *nix commands, but it works for me.

@ehammond perfect, thanks!

I still think what is meant by "list" should be documented in the help

  --filters (list)
          A list of filters used to match properties for Instances. For a com-
          plete reference to the available filter keys for this operation, see
          the Amazon EC2 API reference .

       Shorthand Syntax:

            Key value pairs, where values are separated by commas.
          --filters Name=string1,Values=string1,string2

       JSON Syntax:

          [
            {
              "Name": "string",
              "Values": ["string", ...]
            }
            ...
          ]

perhaps instead of (list) it can say
--filters filter1[ filter2 ... filterN]

I realize this may be an implementation detail of whichever opt parser is used under the covers...

Please note that even the example in the help is wrong. On the aws ec2 describe-images help the example is the following:

       To describe Windows AMIs from Amazon that are backed by Amazon EBS

       This  example describes Windows AMIs provided by Amazon that are backed by Amazon EBS.

       Command:

       aws ec2 describe-images --filters "Name=is-public,Values=true,Name=owner-alias,Values=amazon,Name=platform,Values=Windows,Name=root-device-type,Values=ebs"

The command doesn't do what's in the description. It took me several puzzled looks, and I found the solution only after I found this bug.

@tobiaspal This has been fixed here. This will go out in the next release.

Will look at updating the doc synopsis as well.

Doc synopsis has been updated now. Closing out issue.

The following JSON format appear to work:

aws ec2 describe-tags --region "us-east-1" --filters '
[
  {
    "Name": "resource-id",
    "Values": ["<i-xxx>"]
  },
  {
    "Name": "tag:<MyTag>",
    "Values": ["<true>"] 
  }
]'
aws ec2 describe-tags --region "us-east-1" --filters "Name=resource-id,Values=<i-xxx>,Name=tag:<MyTag>,Values=<true>"

also works but it appears to be OR relationship while the JSON format appears to be AND relationship between the two filters.

This is so confusing! Thank God I found this page!

@michaelxwang I just noticed this too. That inconsistent behavior has got to be a bug...

List the all the Instances With all the Instances Details along with attached Volume Details it must include Volume Name, Volume, and Size

Using this command I can be able to list the volume details with instance id and volume size.

aws ec2 describe-volumes –-query "Volumes[*].[Attachments[0].VolumeId,AvailabilityZone,Attachments[0].InstanceId,Attachments[0].State,Size]" --output text > test.txt

But my query I have to consolidate the whole instance list its includes instance id, instance state, region, platform, key pair name along with attached volumes name, volume id, volume size and mount path name like /dev/sda1,

Please help me with this.

Using this command can be able to get the instance details but I need to single query to get all the details.

aws ec2 describe-instances --filters "Name=instance-state-name,Values=*" --query "Reservations[].Instances[].[Tags[?Key==Name]| [0].Value,InstanceId,State.Name,InstanceType,Placement.AvailabilityZone,PrivateIpAddress,VolumeInfo:BlockDeviceMappings" --output text > instances.txt

In the particular case of querying a tag key and value, you can also do something like this:

aws ssm describe-instance-information --filters "Key=tag:Patch Group,Values=myPatchGroup" --region us-east-1

In that case, 'Patch Group' is a tag key.

Did this formatting just change ?

I had script running successfully - all of a sudden it fails to filter with the OR statement here on the filter.

e.g.

ec2_id1lst1=$(aws ec2 describe-instances --region $region --filter "Name=tag:Name,Values=$ec2_name1upper,Name=tag:Name,Values=$ec2_name1lower" --query 'Reservations[].Instances[].[InstanceId]' --output text | sed '$!N;s/n/ /')

Looks like the OR statement was changed to AND. Is there instruction somewhere on this change and how to use a different OR for the filter?

Did this formatting just change ?

I had script running successfully - all of a sudden it fails to filter with the OR statement here on the filter.

e.g.

ec2_id1lst1=$(aws ec2 describe-instances --region $region --filter "Name=tag:Name,Values=$ec2_name1upper,Name=tag:Name,Values=$ec2_name1lower" --query 'Reservations[].Instances[].[InstanceId]' --output text | sed '$!N;s/n/ /')

Looks like the OR statement was changed to AND. Is there instruction somewhere on this change and how to use a different OR for the filter?

I had a script fail recently as well, this below is currently working ...

  • Ubuntu Bionic AND AMD64
aws ec2 describe-images --owners 099720109477 --filters "Name=name,Values=*ubuntu*bionic*" "Name=architecture,Values=x86_64"
  • Ubuntu Bionic AND AMD64 OR ARM64
aws ec2 describe-images --owners 099720109477 --filters "Name=name,Values=*ubuntu*bionic*" "Name=architecture,Values=x86_64,arm64"
Was this page helpful?
0 / 5 - 0 ratings