Sub topic 12
Fixing expired sessions and configuration mistakes
This chapter assumes you have a working sso-session and profile in ~/.aws/config. It also assumes you can run aws sso login and aws sts get-caller-identity. Most failures come from one of three causes: an expired sign-in, a profile that is not being used, or a configuration typo.
Start with the identity check
Run the same command every time, before you change anything:
aws sts get-caller-identity --profile devThe result tells you which problem you have. An error about a token or session means sign-in. An unexpected account ID or ARN means the wrong profile or credentials. A successful result with the right identity, followed by AccessDenied on another command, means a permissions problem.
Expired or missing sessions
The CLI caches a sign-in token and the role credentials it obtained with it. When the sign-in expires, you see messages such as these:
The SSO session associated with this profile has expired or is otherwise invalid.Error loading SSO Token: Token for ... does not existToken has expired and refresh failed
The fix is the same in each case. Sign in again with the profile that failed:
aws sso login --profile devIf you have two profiles that share one sso-session, one sign-in covers both. If the error persists after a successful login, the profile probably points at a different sso-session name than the one you signed in to.
The wrong identity is being used
Static credentials in environment variables take precedence over a profile in the config file. A leftover AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, or AWS_SESSION_TOKEN from an earlier export will win, even if you pass --profile. This is common after using aws configure export-credentials, because the exported values expire.
Look at where the CLI finds each value:
aws configure list --profile dev
env | grep '^AWS_'In the Type column of aws configure list, env means environment variables are in use. sso means the profile's sign-in is being used. To clear stale values:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKENConfiguration mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| `The config profile (dve) could not be found` | Typo in the profile name or in `AWS_PROFILE` | Compare against `[profile ...]` headers |
| `Unable to locate credentials` | No profile selected and no environment credentials | Set `AWS_PROFILE` or pass `--profile` |
| Message about missing required SSO configuration values | A profile lacks a key such as `sso_account_id` or `sso_role_name` | Add the missing key |
| Login opens the wrong portal | Wrong `sso_start_url` | Correct the URL in the `sso-session` block |
| `AccessDenied` on one service only | The permission set does not allow that action | Ask an administrator to change the permission set |
Two details cause most of the remaining mistakes. First, sso_region is the Region where Identity Center is set up. The region key is the default Region for your commands. They can differ. Second, profile headers in the config file are written [profile dev], but the sso-session header is written [sso-session my-sso] with no profile word.
A clean reset
If nothing else explains the behavior, clear the caches and start again:
aws sso logout
aws sso login --profile dev
aws sts get-caller-identity --profile devExercise
You run aws s3 ls --profile dev right after aws sso login --profile dev succeeded. The output lists buckets from the prod account. What do you check first, and why?
Show answer
Check the environment for AWSACCESSKEYID, AWSSECRETACCESSKEY, and AWSSESSIONTOKEN (for example with env | grep '^AWS_', or aws configure list --profile dev). Credentials from environment variables take precedence over the profile, so an earlier export of prod credentials can override the dev profile. Unset the variables and run aws sts get-caller-identity --profile dev to confirm.