2

I am starting to pick up aws cli and I just wanted to know the difference between --query and --filter in aws cli? When we should use --query and --filter options?

1 Answer 1

2

Essentially --filter is the condition used to select which resources you want described, listed, etc.

On the other hand --query is the list of fields that you want returned in the response. You can do some simple filtering with --query as well but --filter tends to be more powerful.

Example from aws ec2 describe-volumes help:

To describe tagged volumes and filter the output

This example command describes all volumes that have the tag key Name and a value that begins with Test. The output is filtered to display only the tags and IDs of the volumes.

Command:

aws ec2 describe-volumes \
    --filters Name=tag-key,Values="Name" Name=tag-value,Values="Test*" \
    --query 'Volumes[*].{ID:VolumeId,Tag:Tags}'

Output:

[
  {
     "Tag": [ { "Value": "Test2", "Key": "Name" } ],
     "ID": "vol-1234567890abcdef0"
  },
  {
     "Tag": [ { "Value": "Test1", "Key": "Name" } ],
     "ID": "vol-049df61146c4d7901"
  }
]

As you can see the --filter is used to select the required records (i.e. those with tag Name starting with string Test*. And --query is then used to only retrieve the Tags (as Tag) and the VolumeId (as ID).

Hope that helps :)

1
  • this isn't correct. You can imitate that exact filter with: --query 'Volumes[?Tag[?Key==`Name` && starts_with(Value, `Test`)]][].{ID:VolumeId,Tag:Tags}'
    – bazzargh
    Nov 11, 2019 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.