Sub topic 6
Configuring credentials, Regions, and profiles
This chapter assumes you have installed AWS CLI v2 (aws --version works), can run shell commands, and know that an AWS Region is a geographic location where services run. IAM Identity Center sign-in has its own chapter. Here you learn the settings every CLI call depends on and where the CLI looks for them.
What the CLI needs to know
Every CLI call needs three settings:
- Credentials prove who you are. They are an access key ID and secret access key, often with a session token, or an SSO or role-based equivalent.
- Region says where the request goes. Most resources exist in one Region only.
- Output format controls how results print:
json,text,table, oryaml.
Credentials and Region can come from several places. Most "it worked yesterday" problems come from the CLI picking a different source than you expected.
Quick setup with aws configure
The aws configure command asks four questions and saves the answers:
aws configureIt prompts for the access key ID, the secret access key, the default Region, and the default output format. It writes the keys to ~/.aws/credentials and the Region and format to ~/.aws/config. On Windows these files are under %UserProfile%\.aws\.
Use --profile to save settings under a name instead of default:
aws configure --profile devYou can read or change a single value without opening the files:
aws configure set region eu-west-1 --profile dev
aws configure get region --profile dev
aws configure list-profilesThe two files
The CLI keeps settings in two INI-style files. Both are plain text, and you can edit them by hand.
This is ~/.aws/config, which holds non-secret settings:
[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:accessThis is ~/.aws/credentials, which holds long-lived keys:
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
[ci-user]
aws_access_key_id = AKIAI44QH8DHBEXAMPLE
aws_secret_access_key = je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEYThe two files name sections differently:
| File | Default profile | Named profile |
|---|---|---|
| `config` | `[default]` | `[profile dev]` |
| `credentials` | `[default]` | `[dev]` |
Writing [profile dev] in the credentials file, or [dev] in the config file, is a common mistake. The CLI does not find that section and reports that the profile could not be found.
If the same profile has settings in both files, the credentials file wins for keys. Keeping Region and output in config and secrets in credentials keeps the roles of the files clear.
Named profiles
A profile is a named bundle of settings. Profiles let one machine hold several identities, such as a personal sandbox, a work account, and a CI user. You select one in two ways:
aws s3api list-buckets --profile dev
export AWS_PROFILE=dev
aws s3api list-buckets--profile applies to one command. AWS_PROFILE applies to the whole shell session until you unset it. In PowerShell, use $env:AWS_PROFILE = "dev".
A profile can also assume a role. Here the CLI uses the keys in default to call AWS STS and get temporary credentials for the role:
[profile admin]
role_arn = arn:aws:iam::111122223333:role/Admin
source_profile = default
region = us-east-1Add mfa_serial = arn:aws:iam::111122223333:mfa/alice to the profile, and the CLI asks for your MFA code before it assumes the role.
Environment variables
Environment variables override the files, which makes them useful for CI jobs and containers:
| Variable | Purpose |
|---|---|
| `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` | Access key pair |
| `AWS_SESSION_TOKEN` | Required with temporary credentials |
| `AWS_PROFILE` | Selects a named profile |
| `AWS_REGION`, `AWS_DEFAULT_REGION` | Sets the Region |
| `AWS_CONFIG_FILE`, `AWS_SHARED_CREDENTIALS_FILE` | Moves the files to other paths |
How the CLI chooses
For each setting, the CLI uses the first source that provides a value. This is the order for credentials, simplified:
The Region follows a similar order: --region, then AWS_REGION, then AWS_DEFAULT_REGION, then the region value in the active profile. If none exist, most commands fail with "You must specify a region."
Because --region is at the top, you can override the Region for one call without touching any file:
aws ec2 describe-vpcs --region eu-west-1 --profile devSee where a value came from
aws configure list shows the resolved values and their sources:
aws configure list --profile devThe Type column says where each value came from, such as env, config-file, shared-credentials-file, or sso. Secrets are masked. Run this command whenever the CLI behaves unexpectedly.
Confirm the active identity
Settings only matter if they resolve to the identity you intended. Check with STS:
aws sts get-caller-identity --profile devThe response contains UserId, Account, and Arn. Compare the account ID and ARN with what you expect. This call needs no IAM permissions, so it works for any valid identity. Get into the habit of running it before any bulk or destructive command.
Common errors
| Message | Likely cause | Fix |
|---|---|---|
| `Unable to locate credentials` | No source in the chain had credentials | Run `aws configure` or set `AWS_PROFILE` |
| `The config profile (dev) could not be found` | Typo, or the wrong section header style | Check `aws configure list-profiles` and the `[profile x]` vs `[x]` rule |
| `You must specify a region` | No Region in the flag, environment, or profile | Add `--region` or `aws configure set region` |
| `InvalidClientTokenId` or `SignatureDoesNotMatch` | Wrong, deleted, or mistyped keys, such as a trailing space | Re-enter the keys and check for stale `AWS_*` variables |
| `ExpiredToken` | Temporary credentials have expired | Sign in again, or refresh the session token |
| Empty list from a list call | The call went to a different Region than the resources | Re-run with `--region` |
Security habits for stored credentials
- Prefer temporary credentials (IAM Identity Center or role assumption) over long-lived access keys.
- Never commit
~/.aws/credentialsor paste keys into scripts and repositories. - Restrict the credentials file to your user, for example with
chmod 600 ~/.aws/credentials. - Delete access keys you no longer use, and never create keys for the root user.
Exercise
You have a dev profile with region = eu-west-1, and your shell has AWS_DEFAULT_REGION=us-east-1 exported. You run aws ec2 describe-vpcs --profile dev. Which Region does the call use, and how would you send it to ap-southeast-2 for this one call?
Show answer
It uses us-east-1, because the environment variable ranks above the profile's region setting. Add --region ap-southeast-2 to the command, since command-line options rank highest. To make the profile's Region apply again, run unset AWS_DEFAULT_REGION.
Exercise
A teammate's ~/.aws/config contains [dev] followed by region = eu-west-1, and aws s3api list-buckets --profile dev reports that the profile could not be found. What is wrong, and which command confirms the fix?
Show answer
In the config file, a named profile section must be written [profile dev]. Only the credentials file uses a bare [dev]. After correcting the header, aws configure list-profiles should list dev, and aws sts get-caller-identity --profile dev confirms the identity it resolves to.