Sub topic 12
Fixing expired sessions and configuration mistakes
This chapter assumes you have an sso-session and at least one profile in ~/.aws/config, and that you have run aws sso login successfully at least once. You should know what the sign-in token and the role credentials are. The chapter shows how to read the error you get, decide which layer failed, and fix it.
Three layers that can fail
A command such as aws s3 ls --profile dev depends on three things, in this order:
- Configuration. The CLI must find the profile and its
sso_sessionblock. - Sign-in token. The CLI must find a valid access token for that session in its cache.
- Role credentials. The CLI must exchange the token for temporary role credentials, and the role must allow the API call.
Each layer produces different errors. Locating the failing layer first saves most of the debugging time.
Configuration errors
The profile cannot be found
The config profile (prod-readonly) could not be foundThe name you passed to --profile, or the value of AWS_PROFILE, does not match any [profile NAME] header. Check for these causes:
- A typo, or a profile that was created under a different name by
aws configure sso. - A stale
AWS_PROFILEleft over in the shell from earlier work. - A missing
profileprefix. In~/.aws/config, sections are[profile dev]. Only the[default]section omits the wordprofile. In~/.aws/credentials, the prefix is not used.
List what the CLI sees:
aws configure list-profiles
echo "$AWS_PROFILE"
aws configure list --profile devaws configure list shows where each value comes from, such as config-file, env, or manual. This is the quickest way to see whether something other than your file is supplying a value.
The session block does not match
A profile points to a session by name. A mismatch gives a message about a token that does not exist:
Error loading SSO Token: Token for https://example.awsapps.com/start does not existThe usual causes are:
sso_session = my-ssoin the profile, but the block is named[sso-session my_sso].- A different
sso_start_urlthan the one you signed in with. The cache is keyed by session name, so editing the URL after sign-in leaves the old token unusable. - You never signed in for that session. Run
aws sso login --profile dev.
Compare the two blocks side by side:
[sso-session my-sso]
sso_start_url = https://example.awsapps.com/start
sso_region = us-east-1
sso_registration_scopes = sso:account:access
[profile dev]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = ReadOnlyAccess
region = us-west-2Note the two region settings. sso_region is the Region where IAM Identity Center is enabled. region is the default Region for your service calls. Putting your workload Region in sso_region makes sign-in fail, because the CLI looks for your Identity Center instance in the wrong place.
Missing or wrong role details
If sso_account_id or sso_role_name is wrong, sign-in succeeds but the credential exchange fails. The usual message is:
An error occurred (ForbiddenException) when calling the GetRoleCredentials operation: No accessEither the permission set name has a typo, or your user is not assigned that permission set in that account. The role name here is the permission set name, for example ReadOnlyAccess. It is not the longer AWSReservedSSO_... role name that appears in the ARN. Run aws configure sso again, or look in the AWS access portal, to see the exact names offered to you.
Session errors
The session has expired
The SSO session associated with this profile has expired or is not valid.
To refresh this SSO session run aws sso login with the corresponding profile.This is the normal end of a session. The access token has reached the session duration set in Identity Center, or an administrator revoked it. Sign in again:
aws sso login --profile devBecause dev and prod share one sso-session, one sign-in fixes both profiles. If you see the same error after signing in, you probably signed in with a different profile that uses a different session. Run aws configure list --profile dev and check the sso_session value.
For a profile that uses an sso-session block with sso_registration_scopes, the CLI can refresh the access token silently until the refresh token also runs out. An old-style profile has sso_start_url directly in the profile and no sso-session block. It has no refresh, so you sign in more often. Moving to the sso-session format is the fix.
The browser does not open
On a remote host or in a container, the CLI cannot start a browser. Use one of these options:
aws sso login --profile dev --no-browser
aws sso login --profile dev --use-device-code--no-browser prints a URL for you to open on any machine. --use-device-code uses the device flow, where you enter a short code on a separate device. Newer AWS CLI v2 releases default to the authorization code flow with PKCE, so the device flow is opt-in.
Stale environment variables win
A common confusing case is a command that fails with ExpiredToken or The security token included in the request is expired even though you just signed in:
An error occurred (ExpiredToken) when calling the ListBuckets operation:
The provided token has expired.Static credentials in the environment, such as AWS_ACCESS_KEY_ID and AWS_SESSION_TOKEN, rank above AWS_PROFILE in the credential chain. If you earlier ran the export-credentials example from this tutorial, those values are still in your shell after they expire. Check and clear them:
env | grep '^AWS_'
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKENPassing --profile dev on the command line makes the CLI use that profile instead of the environment keys. Setting AWS_PROFILE alone does not.
A broken cache
If the cache is corrupt or points to an old session, clear it and start over:
aws sso logout
aws sso login --profile devaws sso logout deletes the cached access tokens in ~/.aws/sso/cache and the cached role credentials in ~/.aws/cli/cache. It does not change your config file.
Permission errors
An error occurred (AccessDenied) when calling the PutObject operation:
User: arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAccess_abc123/alice
is not authorized to perform: s3:PutObjectSigning in again does not fix this. The login worked, and the role it produced lacks the permission. Run aws sts get-caller-identity --profile dev and read the ARN to see which role you are using. Then check three things:
- The permission set attached to that account assignment allows the action.
- No service control policy or permission boundary blocks the action.
- You picked the right profile. A
ReadOnlyAccessprofile cannot write, anddevandprodmay carry different permission sets.
If an administrator changes a permission set, existing role credentials may keep the old permissions until they expire. Delete the cached role credentials with aws sso logout and sign in again to pick up the change right away.
Clock and network errors
Signed requests include a timestamp. If your machine clock is more than a few minutes off, you may see:
An error occurred (SignatureDoesNotMatch) ...
Signature expired: 20261126T170000Z is now earlier than 20261126T180500ZFix this by syncing the clock with NTP. This is common in virtual machines that resume from sleep. Proxy and firewall problems appear as connection timeouts to *.awsapps.com or portal.sso.<region>.amazonaws.com. The CLI must reach both the sign-in endpoints and the service you call.
A repeatable debugging routine
When the cause is not obvious, work through the same steps each time:
- Run
aws configure list-profilesto confirm the profile exists. - Run
aws configure list --profile devto see where each value comes from. - Run
env | grep '^AWS_'to find overriding variables. - Run
aws sts get-caller-identity --profile dev --debug 2> debug.log. Search the log for the credential provider that was used and the HTTP status of the failing call. - Run
aws sso logout, thenaws sso login --profile dev, and repeat the command.
Quick reference
| Symptom | Likely layer | First fix |
|---|---|---|
| `config profile could not be found` | Configuration | Check name with `aws configure list-profiles` |
| `Token ... does not exist` | Configuration or token | Match session names, then `aws sso login` |
| `SSO session ... has expired` | Token | `aws sso login --profile NAME` |
| `ForbiddenException ... No access` | Role credentials | Check account ID and permission set name |
| `ExpiredToken` after fresh login | Environment | `unset` the `AWS_*` key variables |
| `AccessDenied` on an action | Permissions | Review the permission set |
| `Signature expired` | Clock | Sync system time |
Exercise
You signed in with aws sso login --profile dev five minutes ago. aws sts get-caller-identity --profile dev works, but aws s3 ls in the same shell fails with ExpiredToken. AWS_PROFILE is not set. What is the most likely cause, and how do you confirm and fix it?
Show answer
Without --profile, the CLI falls back to the default credential chain, and the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN variables in the environment come first. They are probably stale values exported earlier, for example by aws configure export-credentials. Confirm with env | grep '^AWS_'. Fix by running unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN, then set AWS_PROFILE=dev or pass --profile dev.
Exercise
After an administrator adds s3:PutObject to your permission set, aws s3 cp still returns AccessDenied. What do you do?
Show answer
The cached role credentials were issued before the change. Run aws sso logout to clear the cache, then aws sso login --profile dev and retry. If it still fails, check aws sts get-caller-identity to confirm you are using the expected account and role, and look for a service control policy or permission boundary that blocks the action.