AWS CLI basics: profiles, regions, and JMESPath queries
coreintermediateThe AWS CLI wraps every service API as a command. A named profile picks which credentials and region to use, and `--query` filters a large JSON response down to just the fields you need with JMESPath.
Think of it as
A universal remote with one button per API call, and a profile switch on the side that decides which account and region the button presses land in.
What we're doing: List running instance IDs only, instead of the full describe-instances response.
- 2
- The JMESPath filter [?State.Name=='running'] runs client-side, after AWS returns every instance regardless of state.
Why this works: describe-instances returns a deeply nested structure — Reservations containing Instances — that is rarely useful to read as-is. --query reshapes it into exactly the list a script needs, without a separate parsing step.
Assuming --region on one command changes the default for later commands
Wrong
Better
What you see: A second command in the same session returns instances from the wrong region, with no error to indicate why.
Why: --region is a per-invocation flag, not session state — the CLI has no memory between commands. Only an environment variable, a profile's configured region, or the flag itself (repeated) determines each call's region.
- Whole: aws ec2 describe-instances \ --profile staging --region eu-west-1 \ --query "Reservations[].Instances[].InstanceId" --output text
- --profile staging — profile: which credentials and default settings to use
- --region eu-west-1 — region: overrides the profile's default region, this call only
- --query "Reservations[].Instances[].InstanceId" — query: JMESPath — reshapes the response client-side
- --output text — output: the response format
Common AWS CLI flags
Together
Remember: --profile picks credentials and default region; --query (JMESPath) reshapes the response client-side — always safer to script against than text-output column position.
See also: sdk clients and resilience

