Sub topic 9
What the CLI caches and where
This chapter assumes you have AWS CLI version 2 installed, can edit ~/.aws/config, and have already run aws sso login at least once. You should know what an sso-session block and a profile are.
Two layers of cache
The CLI stores two kinds of credential material after you sign in. Knowing which is which explains most "why did that still work?" and "why did that stop working?" moments.
| Layer | What it is | Where it lives | Created by |
|---|---|---|---|
| SSO access token | Proves you signed in to IAM Identity Center. It can be exchanged for role credentials. | `~/.aws/sso/cache/` | `aws sso login` |
| Role credentials | Temporary access key, secret key, and session token for one account and role. | `~/.aws/cli/cache/` | The first command that needs them |
The access token is the long-lived layer. Role credentials are short-lived and are derived from the token, one set per profile's account and role.
How a command uses the cache
When you run a command with an SSO profile, the CLI follows these steps.
- It looks in
~/.aws/cli/cache/for role credentials that have not expired. - If none are valid, it reads the access token from
~/.aws/sso/cache/. - It calls IAM Identity Center with that token to get new role credentials for the profile's
sso_account_idandsso_role_name. - It saves the new credentials and runs your command.
If the access token is missing or expired, step 3 fails and the CLI asks you to sign in again.
Looking at the cache
You can list both directories to see what exists. Do not paste their contents into chats or tickets.
ls -l ~/.aws/sso/cache/
ls -l ~/.aws/cli/cache/The files in sso/cache are JSON. Some hold the client registration for your sso-session, and one holds the access token with an expiresAt timestamp. You can check that timestamp without printing the token itself:
for f in ~/.aws/sso/cache/*.json; do
python3 -c "import json,sys; d=json.load(open(sys.argv[1])); print(sys.argv[1], d.get('expiresAt'))" "$f"
doneFiles without an expiresAt field print None. That is normal.
How the files are named
The cache file names are hashes, so you cannot tell which file belongs to which profile by looking at the name.
- With an
sso-sessionblock, the access token file name comes from a hash of the session name. All profiles that sharemy-ssotherefore share one token file. - Role credentials are keyed by account, role, and start URL, so
devandprodeach get their own entry incli/cache.
This is why one aws sso login --profile dev also unlocks prod when both use the same sso-session. Both profiles find the same access token and exchange it for their own role credentials.
Token refresh
An sso-session configured with sso_registration_scopes can refresh its access token silently. The CLI holds a refresh token in the cache and uses it when the access token expires, so you are not sent back to the browser at each expiry. Once the refresh token itself is no longer valid, which depends on the session duration set in IAM Identity Center, you need aws sso login again.
Role credentials do not refresh in place. When they expire, the CLI gets a new set using the access token.
Security notes
- Both directories contain bearer material. Anyone who can read the files can act as you until they expire.
- The CLI creates them with owner-only permissions. Do not loosen them.
- Do not commit your home directory or copy
~/.awsto shared machines or container images. - To remove everything, use
aws sso logout, covered in a later chapter.
Exercise
You run aws sso login --profile dev. Later you run aws s3 ls --profile prod, and both profiles use the same sso-session. Why does the second command work without another browser sign-in, and which cache directory gets a new file?
Show answer
Both profiles share one access token in ~/.aws/sso/cache/, because that file is keyed by the sso-session name. The CLI uses that token to request role credentials for the prod account and role, and it saves them as a new file in ~/.aws/cli/cache/.
Exercise
A teammate deletes everything in ~/.aws/cli/cache/ to "log out", then runs an AWS command and it succeeds. Explain why.
Show answer
The access token in ~/.aws/sso/cache/ is still valid, so the CLI exchanges it for fresh role credentials and recreates the cache entry. Signing out requires removing the access token, which aws sso logout does.