Sub topic 6
Configuring credentials, Regions, and profiles
This chapter assumes you have installed AWS CLI v2 and can run aws --version. You should also know what an IAM identity is and how a Region differs from an account.
What the CLI needs to know
Every CLI call needs two things: who you are (credentials) and where to send the request (a Region). You can set both once and reuse them, or override them per command.
Setting up with aws configure
Run aws configure and answer the four prompts: access key ID, secret access key, default Region, and default output format. The CLI saves your answers into two files in ~/.aws/.
| File | Holds | Example keys |
|---|---|---|
| `~/.aws/config` | Settings | `region`, `output`, `sso_session` |
| `~/.aws/credentials` | Secrets | `aws_access_key_id`, `aws_secret_access_key` |
On Windows these files are in %UserProfile%\.aws\. Keep long-lived keys out of these files where you can. The security chapter explains why, and the next-but-one chapters cover IAM Identity Center as the better option.
Named profiles
A profile is a named bundle of settings. The default profile is used when you name no other. In config, other profiles are written [profile name]. In credentials, the same profile is written [name] with no profile prefix, which is a common source of confusion.
[default]
region = us-east-1
output = json
[profile dev]
region = eu-west-1
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = ReadOnly
[sso-session my-sso]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = us-east-1
sso_registration_scopes = sso:account:accessA matching credentials file for a key-based profile looks like this. The values are the placeholder keys from the AWS documentation.
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYChoose a profile for one command with --profile dev. To use it for a whole shell session, set an environment variable.
export AWS_PROFILE=dev
aws sts get-caller-identityWhere settings come from
The CLI looks in several places and uses the first value it finds. The order, from highest priority to lowest, is:
The practical effect is that environment variables beat files, and flags beat everything. If you export AWS_ACCESS_KEY_ID in a shell and forget about it, the CLI ignores your profile's credentials. That is a frequent cause of "wrong account" surprises.
Checking and changing values
Always confirm which identity a profile resolves to before you run anything that changes resources.
aws sts get-caller-identity --profile dev
aws configure get region --profile dev
aws configure set region eu-central-1 --profile devget-caller-identity returns the account ID and the ARN of the identity making the call. It needs no special permissions, so it works for any valid credentials.
Region behavior
Most resources live in one Region. A command with the wrong Region shows an empty list rather than an error, which looks like missing resources. Override the Region for a single call with --region.
aws ec2 describe-instances --region us-west-2 --profile devExercise
You run export AWS_ACCESS_KEY_ID=... in your shell, then run aws s3api list-buckets --profile dev. Which credentials does the CLI use, and how can you confirm it?
Show answer
The CLI uses the exported environment variable credentials, because environment variables rank above profile files. Confirm with aws sts get-caller-identity --profile dev and check the account ID and ARN. To go back to the profile, run unset AWS_ACCESS_KEY_ID (and the matching secret and session token variables).