Configuration has precedence
The AWS CLI syntax is aws <service> <command> [options], for example aws s3 ls or aws ec2 describe-instances. aws configure writes credentials to ~/.aws/credentials and Region/output settings to ~/.aws/config. Named profiles separate environments and are selected with --profile or AWS_PROFILE.
flowchart LR P["Profile dev<br/>or IAM role"] --> C["AWS CLI v2"] R["Region<br/>ap-southeast-1"] --> C O["Output<br/>json, text, table"] --> C C --> API["AWS API<br/>signed request"] API --> Q["--query<br/>pick fields"] %% step P: Credentials come from the selected profile, environment variables or an attached role. %% step R: Regional commands need a Region from --region, AWS_REGION or the profile. %% step O: --output only changes how results are printed. %% step C: The CLI combines these settings and signs the request. %% step API: The service answers in JSON. %% step Q: --query (JMESPath) filters the answer on your side before printing.
Files and profiles
# ~/.aws/config
[default]
region = ap-southeast-1
output = json
[profile dev]
region = ap-southeast-1
# ~/.aws/credentials
[dev]
aws_access_key_id = AKIA...
aws_secret_access_key = ...
In config, non-default sections are written [profile dev]; in credentials they are just [dev]. Create a profile with aws configure --profile dev. Settings resolve in this order: command-line option, environment variable, profile file.
On EC2 with an IAM role, do not run aws configure to store static keys: static credentials can take precedence and lose automatic rotation. If a regional command gets no Region from an option, environment, or profile, the CLI asks you to specify one.
Reading output
Most commands return JSON. Three tools make it usable:
--output tableortextfor people and shell scripts.--queryto select fields, for example--query "Reservations[].Instances[].[InstanceId,State.Name]".--filtersto let the service filter before sending, for example--filters Name=instance-state-name,Values=running.
--filters reduces what AWS returns; --query only trims what is printed. Large results are paginated automatically; --max-items limits them.
Safe habits
- Run
aws sts get-caller-identityto confirm the account and principal before you change anything. - Many commands support
--dry-run(EC2) to check permissions without making changes. aws <service> helporaws <service> <command> helpshows every option.- Install AWS CLI version 2; version 1 is legacy.
- Turn on auto-prompt with
aws --cli-auto-promptwhen you are learning the parameters.
--output changes presentation, not the Region.
| Command | Purpose |
|---|---|
aws --version |
View the CLI version |
aws sts get-caller-identity --profile dev |
Verify the dev profile |
aws ec2 describe-instances --region ap-southeast-1 --output table |
Select Region and format |
aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" --output text |
Print only instance IDs |
aws configure list-profiles |
List configured profiles |
Traps: writing [profile dev] in the credentials file; expecting --output to change the Region; forgetting which profile is active and changing the wrong account.