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 explains how the AWS CLI chooses a profile, how it then chooses a source of credentials, and how to see what it picked. Most "wrong account" surprises come from this logic.
Two separate decisions
The CLI makes two decisions on every command.
- Which profile is active? A profile is a named group of settings in
~/.aws/config, such asdevorprod. - Where do the credentials come from? The CLI checks a fixed list of sources in order and uses the first one that supplies credentials.
People often assume the second decision always follows the first. It does not. Some sources are checked before the profile's own settings, so they can hide the profile you selected.
How the CLI selects a profile
The CLI picks the profile name from the first of these that is set:
- The
--profileoption on the command. - The
AWS_PROFILEenvironment variable. - The
AWS_DEFAULT_PROFILEenvironment variable. This is older, andAWS_PROFILEis preferred. - A profile named
default.
If none of these exist, the CLI has no profile name. It then falls through to the other credential sources, such as environment keys or an instance role.
# One command, one profile
aws s3 ls --profile prod
# Every later command in this shell
export AWS_PROFILE=dev
aws s3 ls
# The option still wins over the variable
aws s3 ls --profile prodThe last command uses prod even though AWS_PROFILE is dev. To list the profiles the CLI can see, run:
aws configure list-profilesThe `default` profile
A profile named default is used when nothing else selects one. In ~/.aws/config it is written as [default]. Every other profile is written as [profile name]. Forgetting the profile keyword is a common mistake. A block named [dev] in the config file is ignored, so the CLI reports that the profile cannot be found.
If you work with several accounts, consider not defining default at all. A command with no profile selected then fails instead of quietly running against an account you did not intend.
The credential resolution order
Once the CLI has a profile, or has found none, it looks for credentials in a fixed order. For AWS CLI version 2 the order is roughly as follows. The AWS documentation is the authority on the exact order for your version.
| Order | Source | Typical use |
|---|---|---|
| 1 | Command-line options such as `--profile` | Choosing a profile for one command |
| 2 | Environment variables `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN` | Exported or injected credentials |
| 3 | Assume-role and web identity settings in the profile | Role chaining, Kubernetes service accounts |
| 4 | IAM Identity Center settings in the profile (`sso_session`, `sso_account_id`, `sso_role_name`) | Interactive sign-in from a workstation |
| 5 | Static keys in `~/.aws/credentials` or `~/.aws/config` | Legacy IAM user keys |
| 6 | `credential_process` | Credentials produced by an external program |
| 7 | Container credentials | Amazon ECS tasks |
| 8 | Instance profile credentials | Amazon EC2 instances |
The first source that yields credentials wins, and later sources are never consulted. The same logic runs on a laptop, in a container and on an EC2 instance, so the same command works in all three.
The environment-variable trap
Environment keys rank above profile settings. If AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set in your shell, they are used even when AWS_PROFILE=dev is also set. The profile is effectively ignored for credentials, although it can still supply the region.
This often happens after you export credentials for another tool and forget to clear them. The commands succeed, but they run as the wrong identity. Passing --profile on the command line is stronger than the environment keys, so aws sts get-caller-identity --profile dev ignores them. You can also clear the variables:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKENStatic keys have the same effect. If a profile contains aws_access_key_id and also SSO settings, the result is confusing. Keep SSO profiles free of static keys.
What happens inside an SSO profile
When the chosen profile has the sso_session setting, the CLI works out the credentials in a few steps.
- It reads the
sso-sessionblock that the profile names, which gives the start URL and the SSO region. - It looks for a valid cached SSO access token for that session. If there is none, or it has expired, the command fails and tells you to run
aws sso login. - It sends the token to the IAM Identity Center portal service, with the profile's
sso_account_idandsso_role_name. The service returns temporary role credentials: an access key ID, a secret key and a session token. - It caches those role credentials, then signs your request with them.
Because the token belongs to the session, not the profile, two profiles that share one sso-session block (like dev and prod in this tutorial) need only one sign-in. Each profile still gets its own role credentials for its own account.
Seeing the resolved result
Do not guess which source won. Ask the CLI:
aws configure list --profile devTypical output looks like this. The exact masking and column spacing vary by version.
Name Value Type Location
---- ----- ---- --------
profile dev manual --profile
access_key ****************ABCD sso
secret_key ****************wxyz sso
region us-west-2 config-file ~/.aws/configThe Type column names the source of each value. sso means the credentials came from IAM Identity Center. env means they came from environment variables, which is the trap above. shared-credentials-file means static keys. iam-role means an instance profile.
The Location column of the profile row tells you how the profile was chosen. It shows --profile, AWS_PROFILE or None. If you see env on the credential rows when you expected sso, look for exported keys.
The definitive check is still aws sts get-caller-identity, covered in the previous chapter. configure list shows where credentials came from, and get-caller-identity shows what they actually are.
Region resolution is separate
The Region is resolved on its own, in this order:
- The
--regionoption. - The
AWS_DEFAULT_REGIONenvironment variable. - The
regionsetting in the active profile.
The sso_region in the sso-session block is only where IAM Identity Center itself lives. It does not set the Region for service calls. If you see "You must specify a region", add region to the profile. A profile in us-west-2 that is signed in through an Identity Center instance in us-east-1 is normal.
Changing where the CLI looks
Two variables relocate the files, which is useful for tests and for keeping projects separate:
export AWS_CONFIG_FILE=$HOME/projects/acme/aws-config
export AWS_SHARED_CREDENTIALS_FILE=$HOME/projects/acme/aws-credentialsIf aws configure list-profiles shows fewer profiles than you expect, check whether AWS_CONFIG_FILE is set in your shell.
Common mistakes
- Wrong section header.
[dev]instead of[profile dev]in the config file. - Stale exported keys. An earlier
exportof access keys, orevalofexport-credentials, overridesAWS_PROFILE. Unset them or use--profile. - Profile typo. The CLI reports that the config profile could not be found. Compare the name with
aws configure list-profiles. - Missing sign-in. The profile is correct but the SSO token is absent or expired. The error asks you to run
aws sso login. - Mixed settings. A profile that combines
role_arnwith SSO settings, or SSO settings with static keys, resolves through the earlier source in the order. This may not be the one you intended.
Exercise
Your shell has AWSPROFILE=dev and also AWSACCESSKEYID and AWSSECRETACCESS_KEY exported from an earlier task. You run aws sts get-caller-identity and see an unexpected IAM user ARN instead of the dev role. Why, and what are two ways to get the dev role?
Show answer
Environment-variable keys rank above profile settings in the resolution order, so the CLI used them and ignored the SSO settings of the dev profile. You can get the dev role by unsetting AWSACCESSKEYID, AWSSECRETACCESSKEY and AWSSESSIONTOKEN, or by passing --profile dev on the command, which takes precedence over the environment keys. Running aws configure list would have shown env in the Type column.
Exercise
A profile is defined in ~/.aws/config as [dev] with ssosession, ssoaccountid and ssorole_name. aws s3 ls --profile dev reports that the profile cannot be found. What is wrong?
Show answer
Named profiles in the config file must be written [profile dev]. Only the default profile omits the word profile. The [dev] block is ignored, so the CLI finds no such profile.