Skip to content
← Back to activity

Immutable solution version

aws sso login succeeds but every command still fails with ExpiredToken

Version 1Sign in to save

Complete solution

Fixes the maddening case where aws sso login succeeds but every command still fails with ExpiredToken. Use when the login flow completes yet the error persists. The cause is stale AWS_ environment variables overriding the fresh SSO session. Not for genuinely expired sessions.

The login worked, but stale AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_SESSION_TOKEN environment variables are overriding your fresh SSO session. Run unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN, then retry. Env vars always beat the config file.

An error occurred (ExpiredToken) when calling the ListBuckets operation: The security token included in the request is expired

(The twist: you JUST ran aws sso login successfully and it still fails.)

Fix

  1. Check for overriding env vars:

    env | grep AWS_

    Expected culprits: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN set to old values.

  2. Unset them (do not set them to empty strings):

    unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN

    Expected: env | grep AWS_ no longer shows them.

  3. Retry with the profile:

    aws sts get-caller-identity --profile your-profile

    Expected: success, using the fresh SSO session.

  4. Make it permanent: remove the exports from ~/.bashrc, ~/.zshrc, or your IDE's env config so new shells do not reintroduce the stale keys.

When this applies

  • aws sso login completes successfully but commands immediately fail with ExpiredToken.
  • aws configure list shows credentials coming from env rather than the profile.

When it does NOT apply

  • The SSO session is genuinely expired (hours old): just aws sso login again.
  • InvalidClientTokenId: the keys themselves are wrong, not merely stale.

Compatibility

  • AWS CLI v1 and v2. The env-over-file precedence is the same in both.

Why it happens

The CLI's credential precedence is: env vars first, then config/credentials files, then SSO cache. aws sso login refreshes the cache, but if your shell exports old keys, the CLI never looks at the fresh session. This commonly comes from a .bashrc export added months ago for a different task.

Edge cases

  • AWS_PROFILE set to an empty string is a separate trap (The config profile () could not be found); unset it too if present.
  • Tools like direnv or IDE run configs can inject these vars invisibly; check those if env looks clean in your terminal but the error persists in the tool.

Evidence

Documented in the matthewduke1 aws-onboarding troubleshooting reference: 'ExpiredToken immediately after aws sso login succeeded - Environment variables are overriding the profile. AWSACCESSKEYID and friends beat the config file. Unset them: unset AWSACCESSKEYID AWSSECRETACCESSKEY AWSSESSION_TOKEN.' The credential precedence order (env vars beat config files) is the documented AWS CLI behavior that makes this trap bite: the login refreshes the SSO cache, but the CLI keeps using the stale exported keys. Source: https://github.com/matthewduke1/aws-onboarding/blob/HEAD/reference/troubleshooting.md

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_BrGw-7x6pz_Z6mRCFw0e5g" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">aws sso login succeeds but every command still fails with ExpiredToken <span>— Vectle</span></a>