Sub topic 8
Selecting profiles and how credentials are resolved
This chapter assumes you have an sso-session block and at least one SSO profile in ~/.aws/config, and that you can run aws sso login. It covers how the AWS CLI picks a profile and which credential source wins when several are present.
Choosing a profile
The CLI uses the first of these that is set:
- The
--profileoption on the command. - The
AWS_PROFILEenvironment variable. - The profile named
default, if one exists.
# One command, one profile
aws s3 ls --profile prod
# Every command in this shell
export AWS_PROFILE=dev
aws s3 ls
# --profile still overrides AWS_PROFILE for a single command
aws s3 ls --profile prodTo go back to default, run unset AWS_PROFILE.
The resolution order
Selecting a profile does not guarantee its credentials are used. The CLI walks a chain of sources and stops at the first one that supplies credentials. Simplified, the order for the CLI is:
- Command-line options, such as
--profile. - Environment variables, such as
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEYandAWS_SESSION_TOKEN. - Assume-role and web-identity settings in the profile.
- IAM Identity Center settings in the profile (
sso_session,sso_account_id,sso_role_name). - A
credential_processsetting. - Static keys in
~/.aws/credentialsor~/.aws/config. - Container credentials, then EC2 instance profile credentials.
For this tutorial the useful consequence is that environment variables outrank an SSO profile selected through AWS_PROFILE. If AWS_ACCESS_KEY_ID is still exported from an old session, the CLI uses it and ignores your SSO profile.
Seeing what the CLI chose
Two commands show the active source and identity:
aws configure list --profile dev
aws sts get-caller-identity --profile devaws configure list has a Type column next to each value. sso means the CLI used Identity Center. env means environment variables won. shared-credentials-file or config-file means static keys. Run it first when the identity is not what you expect.
Profiles that share a session
In the two-profile configuration from earlier, dev and prod both point at sso_session = my-sso. They share one sign-in token. They differ in account ID, role name and region, so each profile gets its own role credentials:
aws sso login --sso-session my-sso
aws s3 ls --profile dev
aws s3 ls --profile prodSigning in once with --sso-session authorizes both profiles. You do not need a separate browser sign-in for each.
Common mistakes
| Symptom | Likely cause | Check |
|---|---|---|
| Commands run in the wrong account | Environment keys override the profile | `aws configure list` shows `env` |
| "The config profile (x) could not be found" | Typo in `--profile` or `AWS_PROFILE` | Look for `[profile x]` in `~/.aws/config` |
| Region errors on a valid profile | No `region` set for the profile | Add `region` or pass `--region` |
| Profile works for `default` only | Profile named without the `profile ` prefix | In `config`, write `[profile dev]`, not `[dev]` |
Exercise
You run export AWS_PROFILE=dev, but aws sts get-caller-identity reports the account of an old IAM user. aws configure list shows env in the Type column. What is happening, and what do you do?
Show answer
Environment variables rank above profile settings in the resolution order. The shell still has AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY set, so the CLI uses those and ignores the SSO profile. Unset AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN, then run aws configure list again to confirm the Type is sso.