DevOps Peach

AWS Basics · System Operations on AWS · Lesson 4 of 4

AWS CLI Basics

Configure profiles, Regions, output, and verify AWS CLI identity.

Practice interactively →

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:

--filters reduces what AWS returns; --query only trims what is printed. Large results are paginated automatically; --max-items limits them.

Safe habits

--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.

Practice interactively →