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?

Shash
  • 123
  • 1
  • 5

1 Answers1

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

Nicola Ben
  • 123
  • 4
MLu
  • 24,849
  • 5
  • 59
  • 86
  • 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 '19 at 17:21