Sub topic 11
Signing out and clearing cached credentials
This chapter assumes you have already signed in with aws sso login and know that the CLI caches an access token and role credentials on disk. You should also know how profiles are selected. Here you will learn what aws sso logout removes, what it leaves behind, and how to finish a clean sign-out.
Why sign out at all
Cached credentials are temporary, but they are still usable by anything that can read your home directory until they expire. Signing out shortens that window. It is most useful on shared machines, before handing over a laptop, or after an incident.
What aws sso logout does
aws sso logoutThe command takes no profile argument. It deletes all of the following:
- The cached IAM Identity Center access tokens in
~/.aws/sso/cache. - The cached role credentials that the CLI derived from those tokens, in
~/.aws/cli/cache.
Because the access token is gone, the CLI cannot request new role credentials. The next command that uses an SSO profile fails until you run aws sso login again. You can confirm this:
aws sso logout
aws sts get-caller-identity --profile devThe second command reports that the token is missing or the session has expired, and it asks you to sign in.
What logout does not clear
Three things survive a sign-out. Check each one when you need a complete cleanup.
| Left behind | Why it remains | How to clear it |
|---|---|---|
| Exported credentials | Environment variables are copies held by your shell, not by the CLI cache | Unset them (see below) |
| Browser session | The access portal session belongs to the browser | Sign out of the portal in the browser |
| Config file entries | `sso-session` and profile blocks are settings, not secrets | Leave them. They allow the next sign-in |
The first row matters most. If you ran eval "$(aws configure export-credentials ...)" earlier, your shell still holds working credentials after logout. They stay valid until their own expiry time.
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_CREDENTIAL_EXPIRATION
unset AWS_PROFILEThe second row explains a common surprise. After you sign out of the CLI, the next aws sso login may open the browser and approve immediately without asking for a password. The portal session in the browser is still active. Sign out of the portal as well if you need the full password prompt.
A complete sign-out routine
- Run
aws sso logoutto remove the cached token and role credentials. - Unset any exported credential variables in every shell where you set them.
- Sign out of the access portal in the browser if the machine is shared.
- Verify with
aws sts get-caller-identity. It should fail with a sign-in error.
If AWS_ACCESS_KEY_ID is still set in step 4, the CLI uses it ahead of your profile, and the call succeeds with the exported identity. That is the environment variables in the credential order at work, not a logout failure.
Check your understanding
Exercise
You ran aws sso logout, but aws sts get-caller-identity --profile dev still returns the dev account identity. Name two likely causes and how to check each.
Show answer
First, exported credentials may still be set in the shell. Run env | grep AWS_ and unset AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN. Environment variables are resolved before profile settings, so they win. Second, the profile may use a credential_process that calls aws configure export-credentials and returns cached or freshly obtained credentials from another source. Inspect the profile in ~/.aws/config. In both cases logout worked, but another credential source supplied the identity.