Sub topic 3
What the CLI Caches and Where
Assume you have AWS CLI version 2 installed, an IAM Identity Center user, and an assigned permission set. The CLI uses cached temporary data so you do not need to complete browser sign-in for every command.
Cached tokens and credentials
After aws sso login, the CLI stores an access token in ~/.aws/sso/cache/. It uses this token to request temporary role credentials. Those credentials may be cached under ~/.aws/cli/cache/. The files are local implementation details. Do not edit or commit them.
sequenceDiagram
participant C as AWS CLI
participant S as IAM Identity Center
participant A as AWS account
C->>S: Read cached access token
C->>S: Request role credentials
S->>A: Authorize permission set
A-->>C: Temporary credentials
C->>A: Run AWS commandSession lifetimes
Two timers matter:
- The IAM Identity Center access-token session controls how long the CLI can obtain credentials without another sign-in.
- The permission set's role-session duration controls how long the issued AWS credentials work.
Your administrator sets these limits. When role credentials expire but the access-token session remains valid, the CLI can usually obtain fresh credentials. When the access-token session expires, run aws sso login again.
Signing out and troubleshooting
Use this command to remove cached SSO tokens and related cached credentials:
aws sso logoutThen sign in again with the intended profile:
aws sso login --profile dev
aws sts get-caller-identity --profile devAn expired-session error usually means that you need to sign in again. An access-denied error usually means the permission set or account assignment is wrong. A profile error can mean that AWS_PROFILE, --profile, or the sso-session name points to an unexpected configuration.
Exporting credentials
Some tools cannot read IAM Identity Center profiles. Export temporary credentials into the current shell:
eval "$(aws configure export-credentials --profile dev --format env)"
aws sts get-caller-identityThese environment variables can out-prioritize profile settings in the credential chain. Clear them or open a new shell when switching identities.
Automation and CI
Interactive browser sign-in is a poor fit for unattended jobs. Prefer OIDC federation, a CI provider's role assumption, or another short-lived credential mechanism designed for automation. Do not place exported credentials or cached files in source control.
Exercise
What should you check first when a command uses the wrong AWS account?
Show answer
Check AWS_PROFILE, any --profile option, and the result of aws sts get-caller-identity.