Sub topic 2
Configuring credentials, Regions, and profiles
Assume you know basic JSON, can use a terminal, and have an AWS account with permission to view resources. A CLI profile is a named set of credentials and settings. A Region is the AWS geographic area where a resource exists.
Configuring credentials, Regions, and profiles
AWS CLI v2 reads settings from environment variables, configuration files, and command options. A named profile keeps credentials and defaults separate for different accounts or roles.
aws configure --profile dev
aws sts get-caller-identity --profile devThe first command prompts for an access key, secret key, default Region, and output format. Prefer short-lived credentials from IAM Identity Center or another identity provider. Avoid creating long-lived access keys for the root user.
The CLI stores shared settings in ~/.aws/config. Access keys, when used, normally go in ~/.aws/credentials. Keep both files private.
[profile dev]
region = eu-west-1
output = jsonYou can override a profile's Region or output format for one command:
aws ec2 describe-instances \
--profile dev \
--region us-east-1 \
--output tableaws sts get-caller-identity is the quickest way to verify which identity is active.
Anatomy of an AWS CLI command
The general structure is:
aws service operation [options]For example, ec2 is the service, describe-instances is the operation, and --profile, --region, and --output are options.
Shaping output with `--output`, `--query`, and `--filters`
--output controls presentation. Common values are json, yaml, text, and table. --query uses JMESPath, a language for selecting fields from JSON responses. A service-side --filters option reduces what AWS returns before the response reaches the CLI.
aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[].Instances[].{Id:InstanceId,Type:InstanceType}' \
--output tableLaunching AWS CloudShell
CloudShell is a browser-based terminal opened from the AWS Management Console. It authenticates with your current console identity and includes AWS CLI v2. Its home directory persists between sessions in a Region, but the shell environment is not a replacement for durable source control or backups. Use it when you need a temporary, authenticated terminal without installing the CLI locally.
Exercise
Which option should you use to verify the account before running a destructive command?
Show answer
Run aws sts get-caller-identity with the same --profile and --region context.