Sub topic 8
Shaping output with --output, --query, and --filters
This chapter assumes you have AWS CLI v2 installed, a working profile, and a basic understanding of JSON. If aws sts get-caller-identity returns your identity, you are ready.
Most AWS CLI calls return JSON, and the raw response is often much larger than what you need. Three options control what you see: --output picks the format, --query trims and reshapes the result on your machine, and --filters asks the service to return fewer items in the first place.
Choosing an output format
The --output option accepts these values:
| Format | Best for |
|---|---|
| `json` | Default. Passing results to tools such as `jq`. |
| `text` | Tab-separated values for shell loops and variables. |
| `table` | Reading results in a terminal. |
| `yaml` | Human-readable structured output. |
You can also set a default with output = json in your profile. The command-line option overrides it.
Trimming results with --query
The --query option takes a JMESPath expression. JMESPath is a query language for JSON. The CLI applies it after the response arrives, so it never reduces what the service sends.
This command prints only the bucket names:
aws s3api list-buckets --query 'Buckets[].Name' --output textBuckets selects the list. [] flattens it so the next part applies to every element. .Name takes the Name field from each one.
To build a new object for each item, use braces with your own key names:
aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[].Instances[].{Id:InstanceId,Type:InstanceType}' \
--output tabledescribe-instances nests instances inside reservations, which is why Reservations[].Instances[] appears. The braces create an object with the keys Id and Type.
Filtering on the server with --filters
--filters is not a general CLI option. It exists only on operations that support it, such as many EC2 describe-* calls. The service applies the filter before it responds. The format is Name=...,Values=....
In the instance example above, Name=instance-state-name,Values=running makes EC2 return only running instances. The --query then reshapes what remains.
The two options do different jobs, and the difference matters:
--filtersreduces the data transferred and the number of pages returned. Filter names are defined by each service.--queryreshapes the response locally. It works on any command.
Where a service offers a filter, use it first and add --query for the final shape.
Common mistakes
- Using
--output textwith a query that returns nested objects. Text output flattens them, which makes the result hard to parse. Usejsonfor nested data. - Expecting
--queryto reduce the amount of data fetched. It does not. - Forgetting that a query with no match returns
nullin JSON (orNonein text output), not an error. - Querying a single page. With pagination,
--queryruns on each page, which can give surprising results. The next chapters cover pagination.
Exercise
You want only the IDs of running EC2 instances, one per line, as plain text. Which options would you combine, and which one should do the "running" selection?
Show answer
Use --filters "Name=instance-state-name,Values=running" so EC2 returns only running instances, then --query 'Reservations[].Instances[].InstanceId' and --output text. The filter does the selection on the server. The query only picks the field, and the output format sets the plain-text layout.