The AWS CLI --query option filters the JSON response on your computer using a JMESPath expression, so you can keep only the fields you need. For example, aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" prints just the instance IDs. Combine it with --output text or --output table to get results that scripts and humans can read easily.
The filtering is client-side: the service still returns the full response, and the CLI trims it afterwards. That is why --query shapes output but does not reduce what AWS does or charges for.
Most AWS CLI responses are JSON documents. --query takes a JMESPath expression, runs it against that document, and prints the result in the format chosen by --output.
Consider a trimmed response from aws ec2 describe-instances:
Everything below uses this shape. Wrap expressions in quotes; on Linux and macOS use double quotes for the whole expression and single quotes for string literals inside backticks or quotes, as shown later.
Use dots to descend into objects and [] to flatten a list.
Reservations[] visits every reservation, Instances[] every instance inside, and InstanceId picks one field. Without the empty brackets you would get a nested list of lists. To get a single item use an index: Reservations[0].Instances[0].InstanceId, and [-1] selects the last element.
Square brackets create an array of chosen fields; curly braces create an object with names you choose.
With --output table, object keys become column headings, which makes the second form the best choice for readable tables. Remember that table output sorts object keys alphabetically.
A filter expression [?condition] keeps only items that match. Use backticks around literal numbers and single quotes around strings.
Supported comparisons are ==, !=, <, <=, >, >=, combined with &&, || and !. Useful functions include contains(), starts_with(), ends_with(), length(), sort_by(), max_by() and join().
The & before a field name creates an expression reference, which sort_by and max_by need. Slices such as [-5:] work like Python slices.
--output text removes quotes and brackets, so a single value can go straight into a shell variable.
If the query matches nothing, text output prints None. Handle that case in scripts, or use --output json with jq when you need stricter parsing. A related warning: a query that produces a list in text mode separates items with tabs and newlines, which can surprise loops.
Many describe- operations also accept --filters, which the service applies before returning data.
| Option | Where it runs | Best for |
|---|---|---|
--filters | On the AWS service | Reducing the data returned, by tag, state or ID |
--query | In the CLI, after the response | Choosing fields and shaping output |
Use both: filter on the server to keep responses small, then query on the client for the columns you want. The CLI also paginates automatically; --query is applied to each page unless you disable pagination, and --max-items limits the total number returned.
[, ? or * as globbing characters.'running') or backticks.--query to save bandwidth or cost; it only changes what is printed.null because a key name is misspelt, since JMESPath is case-sensitive.JMESPath, a JSON query language with a published specification. The same expressions work in many languages and tools that include a JMESPath library.
Select it with an index or flatten expression and set --output text, for example --query "Reservations[0].Instances[0].InstanceId" --output text.
No. --query is built into the CLI and uses JMESPath, while jq is a separate program with its own syntax. Both filter JSON; --query has no extra dependency.
The path matched nothing, usually because of a typo, wrong case or a list that needs []. Print the full output first to inspect the real structure.
--query applies a JMESPath expression to the response: dots walk objects, [] flattens lists, [a,b] and {k:v} build rows and objects, and [?...] filters. Pair it with --output text for scripts or --output table for reading, and use --filters when you want the service itself to return less data.