Sub topic 9
What the CLI caches and where
This chapter assumes you have already run aws sso login successfully. It also assumes you can read a JSON file and use a Unix shell. After sign-in, the AWS CLI version 2 keeps two kinds of cached data on disk. Knowing which is which explains why a command sometimes runs without a browser prompt, why a "sign in again" error appears, and what aws sso logout deletes.
Two caches, two jobs
The CLI keeps these items separately:
- The SSO access token. This is proof that you signed in to IAM Identity Center. It is not an AWS credential. It cannot call S3 or EC2. It can only ask Identity Center for role credentials.
- The role credentials. These are the access key ID, secret access key and session token for the role in one account. The CLI gets them by presenting the access token and naming an account and a role. Your service calls are signed with these.
Each cache has its own directory and its own expiry time.
| Item | Directory | Written by | Used for |
|---|---|---|---|
| SSO access token (and refresh token) | `~/.aws/sso/cache/` | `aws sso login` | Requesting role credentials |
| Client registration | `~/.aws/sso/cache/` | `aws sso login` | Identifying the CLI to Identity Center |
| Role credentials | `~/.aws/cli/cache/` | Any command that needs credentials | Signing AWS API requests |
On Windows, the same directories sit under %UserProfile%\.aws\.
The SSO token cache
aws sso login opens your browser, you approve the request, and the CLI writes JSON files into ~/.aws/sso/cache/. List them:
ls -l ~/.aws/sso/cache/You will see file names made of 40 hexadecimal characters, ending in .json. For a profile that uses an sso-session block, the name is the SHA-1 hash of the session name. You can reproduce it. Using the session name my-sso from the earlier chapters:
printf '%s' my-sso | sha1sumThis is why two profiles that point to the same sso-session share one sign-in. They look up the same file. A profile in the older legacy format has no session block. It names the start URL directly, and the file name is derived from that URL instead.
A token file holds secrets, so avoid printing the whole thing. Select only the harmless fields:
jq '{startUrl, region, expiresAt}' ~/.aws/sso/cache/*.jsonThe file for an sso-session usually contains these fields:
accessTokenis the bearer token. Treat it like a password.expiresAtis the time the access token stops working, in UTC.refreshTokenlets the CLI get a new access token without opening a browser.clientIdandclientSecretidentify the client registration that the CLI created for itself.registrationExpiresAtis when that registration lapses.startUrlandregionrecord which Identity Center instance issued the token.
The refreshToken is present only because the session block requested the sso:account:access scope through sso_registration_scopes. A legacy profile with no session block gets no refresh token. When its access token expires, you must sign in again by hand. The sso-session format lets the CLI renew the token on its own while the Identity Center session is still valid.
The role credential cache
The second cache is in ~/.aws/cli/cache/. The CLI fills it the first time a command needs credentials for a profile:
aws sts get-caller-identity --profile dev
ls -l ~/.aws/cli/cache/Each file is named with a hash that depends on the account, the role and the sign-in source. So dev and prod get different files even though they share one token. Inspect the non-secret parts:
jq '{AccessKeyId: .Credentials.AccessKeyId, Expiration: .Credentials.Expiration, ProviderType}' ~/.aws/cli/cache/*.jsonThe file layout can vary by CLI version and provider. Your output may show different fields, and the file may not have a ProviderType key at all. Do not write scripts that depend on this file format. For stable output, use aws configure export-credentials, which is covered in a later chapter.
The reason for this second cache is cost. Getting role credentials takes a network call to Identity Center. By storing the result, the CLI can run many commands in a row, such as a shell loop with a hundred aws s3 ls calls, with only one such call.
How the CLI decides what to do
Every command that uses an SSO profile follows the same order of checks. The diagram shows it.
Three consequences follow from this order:
- Most commands never touch the network for authentication. The first check passes.
- Role credentials usually expire sooner than the access token. When they do, the CLI renews them silently using the token.
- You see an error only when the last branch is reached. That branch means the token is dead and cannot be refreshed.
The CLI treats credentials as expired slightly before their listed expiry, so it does not hand a request a credential that will fail in flight. A credential with a few minutes left may therefore be refreshed already. This is normal.
Where the cache stops being yours
Identity Center keeps its own session for you on the server. The cache on your disk is only a local copy of tokens issued from it. This gives two properties that surprise people:
- Deleting the cache files does not end your Identity Center session. The next
aws sso loginmay finish instantly, because your browser still has a valid session with the portal. It may not ask for a password at all. - A token copied to another machine works there until it expires. The token is a bearer token. Whoever holds the file can request role credentials for the accounts you can reach.
For that reason, keep the home directory protected. The CLI creates the cache files readable by the owner only. Check this and fix it if needed:
ls -l ~/.aws/sso/cache/ ~/.aws/cli/cache/
chmod 600 ~/.aws/sso/cache/*.json ~/.aws/cli/cache/*.jsonClearing the cache
There are three ways to clear it, and they differ in how much they remove.
Full sign-out. This is the normal choice:
aws sso logoutIt removes the cached SSO access tokens and the role credentials derived from them. The next command needs a new sign-in.
Only the role credentials. If you changed a permission set and want the CLI to fetch fresh role credentials, delete just that cache. The SSO token stays, so no browser step is needed:
rm -f ~/.aws/cli/cache/*.json
aws sts get-caller-identity --profile devEverything by hand. If aws sso logout fails or the cache is corrupted, delete the JSON files in both directories. Do not delete ~/.aws/config. That file is your configuration, not a cache.
Common errors that point to the cache
- "The SSO session associated with this profile has expired or is otherwise invalid." The access token is expired and no refresh is possible. Run
aws sso login --profile <name>. - "Error loading SSO Token: Token for … does not exist." No cache file matches this profile. Either you never signed in, you logged out, or the
sso_sessionname in the profile does not match the session you signed in with. Check the spelling in~/.aws/config. - A new permission seems missing. The cached role credentials were issued before the change. Delete
~/.aws/cli/cache/*.jsonand retry. - Sign-in works but a command still fails. Run
aws sts get-caller-identity --profile <name>to confirm which account and role the cached credentials represent. A profile that points to the wrongsso_account_idproduces access errors that look like permission problems.
The next chapters cover how long these items last and how to troubleshoot in more detail. For now, remember the split: the token proves who you are to Identity Center, and the role credentials prove what you may do in one account.
Exercise
You run aws sso login --profile dev, then aws s3 ls --profile prod, and both succeed. Both profiles use sso_session = my-sso. You then delete only the files in ~/.aws/cli/cache/ and run aws s3 ls --profile prod again. Does the CLI open a browser? Explain which cache is involved at each step.
Show answer
No browser opens. Both profiles share one SSO access token in ~/.aws/sso/cache/, named from the hash of my-sso, so the single login covered prod as well. The first prod command used that token to request role credentials and stored them in ~/.aws/cli/cache/. Deleting that directory removed only the role credentials. On the next command, the CLI finds the access token still valid and requests new role credentials from Identity Center. A browser is needed only if the access token has expired and cannot be refreshed.
Exercise
A teammate says "I deleted my cache files, so nobody can use my old session any more." Give two reasons this claim may be wrong.
Show answer
First, deleting local files does not end the server-side Identity Center session, so a new aws sso login may succeed without a password prompt. Second, if the token or exported role credentials were copied elsewhere before deletion, those copies keep working until they expire, because they are bearer credentials. Only the expiry time, or an administrator ending the session, stops them.