Sub topic 10
How long a session and its credentials last
This chapter explains the three clocks that decide when the AWS CLI asks you to sign in again. It assumes you can already configure an sso-session and run aws sso login, as covered in earlier chapters. It also assumes you know what a permission set is.
Three lifetimes, not one
Signing in with IAM Identity Center does not produce a single credential with a single expiry. Three separate things have their own lifetimes:
- The Identity Center session. This is your sign-in at the Identity Center portal. It is created when you authenticate in the browser. Its length is set by an administrator in the Identity Center settings, and the default is 8 hours. The allowed range is 15 minutes to 90 days.
- The SSO access token. The CLI receives this token after you approve the sign-in. It proves you have an active session. The CLI uses it to ask the portal for role credentials. It is cached on disk.
- The role credentials. These are the temporary access key, secret key, and session token that the CLI sends with each API call. They belong to an IAM role that Identity Center created from your permission set. Their lifetime is set by the permission set's session duration.
The tokens and credentials are layered. The access token is bound to your session. The role credentials are fetched with the access token. When any layer expires, the layers below it stop working.
The Identity Center session
An administrator sets the session duration for the whole Identity Center instance. When you sign in through the browser, the portal starts a session of that length. During the session you can approve new CLI sign-ins without typing your password again, because the browser already holds a valid portal session.
Two points matter in practice:
- The CLI session is not extended by activity. A session of 8 hours ends 8 hours after you authenticated, even if you ran commands a minute ago.
- When the portal session ends, a browser that still has a cookie for it may still approve a new CLI request for a short time. The result depends on the administrator's settings, so do not rely on it. Plan for a fresh sign-in at the end of each session.
The SSO access token and automatic refresh
The way you configured your profile decides how the CLI treats the access token.
A profile that uses an sso_session block follows the newer, token-provider flow. The sso_registration_scopes setting is what enables it. In this flow, the CLI registers itself as an OAuth client and receives an access token plus a refresh token. When the access token is close to expiry, the CLI uses the refresh token to get a new access token without opening the browser. The refresh continues until the Identity Center session itself ends.
A legacy profile puts sso_start_url and sso_region directly in the profile and has no sso_session block. It receives an access token only. There is no refresh. When the token expires, you must run aws sso login again.
| Profile style | Configuration | Refresh token | When you must sign in again |
|---|---|---|---|
| `sso-session` (recommended) | `sso_session = my-sso` plus a `[sso-session]` block | Yes | When the Identity Center session ends |
| Legacy | `sso_start_url` inside the profile | No | When the access token expires |
The access token lifetime is short compared with the session. Because the CLI refreshes it quietly, you usually notice only the session boundary.
The role credentials
The CLI does not use the access token for API calls. It exchanges the token for role credentials through the portal's GetRoleCredentials operation. Those credentials last for the session duration of the permission set. The default is 1 hour. An administrator can set it from 1 hour to 12 hours.
The CLI caches the credentials and reuses them until shortly before they expire. It then fetches new ones automatically, as long as the access token is still valid. So with the defaults you might see this pattern during a working day:
- 09:00: you sign in. The CLI gets an access token and role credentials valid until 10:00.
- 10:05: you run a command. The CLI fetches new role credentials valid until 11:05. You are not prompted.
- 17:00: the 8-hour session ends. The next command fails until you sign in again.
The session duration of the permission set affects only the role credentials. A 12-hour permission set does not extend a 8-hour Identity Center session. If the session ends at 17:00, you cannot fetch new credentials after that time. Credentials that were already fetched can keep working until their own expiry, and that is why a command sometimes succeeds right after the session ends.
[profile dev]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = ReadOnlyAccess
region = us-west-2The profile does not contain any lifetime setting. Both durations come from the service side. You cannot lengthen them in ~/.aws/config.
Seeing what is cached
The CLI stores its state under ~/.aws/sso/cache. The access token and client registration are saved as JSON files. Role credentials are held in a separate CLI cache, so you will not find a file you can edit to extend them.
You can check the expiry of the token by reading the cache file. This sketch uses Python so it works on any platform. It lists each file and the expiresAt value when one is present.
import json
from pathlib import Path
cache_dir = Path.home() / ".aws" / "sso" / "cache"
for path in sorted(cache_dir.glob("*.json")):
data = json.loads(path.read_text())
expires = data.get("expiresAt", "no expiresAt field")
kind = "token" if "accessToken" in data else "registration"
print(f"{path.name}: {kind}, expires {expires}")Treat these files as secrets. The access token can be used to obtain role credentials, so do not paste its contents into tickets or chats.
The role credentials can be inspected without reading files. This command prints them, including the expiration time, in the process format.
aws configure export-credentials --profile dev --format processThe Expiration field in the output shows when the current role credentials stop working. If the cached ones are close to expiry, the command fetches fresh ones first.
What happens at each expiry
The error you see tells you which clock ran out.
| What expired | Typical behavior | What to do |
|---|---|---|
| Role credentials | The CLI fetches new ones silently | Nothing |
| Access token, with a refresh token | The CLI refreshes silently | Nothing |
| Access token, legacy profile | Error that the SSO token is expired or invalid | Run `aws sso login` |
| Identity Center session | Refresh fails, and the CLI asks you to sign in | Run `aws sso login` |
A common message is Error loading SSO Token: Token for https://example.awsapps.com/start does not exist or The SSO session associated with this profile has expired or is otherwise invalid. To refresh this SSO session run aws sso login with the corresponding profile. Both mean the CLI cannot get a valid token and needs a new browser sign-in.
A long-running command can fail partway through. For example, a script that runs for several hours reads credentials once when it starts, and some tools do not re-read them. If the tool holds one set of credentials for longer than the permission set's duration, calls begin to fail with ExpiredToken. Exported credentials, covered in a later chapter, have this limit in a stricter form, because nothing refreshes them.
Choosing durations
Both values are set by administrators, but you may be asked for input, so it helps to know the trade-offs.
- A short Identity Center session (for example 8 hours or less) limits how long a stolen access token is useful. It costs the user a daily sign-in.
- A long Identity Center session (days) reduces sign-ins. It lengthens the window in which a stolen token can be used to fetch credentials. Pair it with MFA and short permission set durations.
- A short permission set duration (1 hour) limits damage from leaked role credentials. The CLI hides the cost by refreshing automatically.
- A long permission set duration (up to 12 hours) helps tools that cannot refresh, such as a long job started with exported credentials. Use it only for the permission sets that need it, because it applies to everyone assigned that permission set.
Changing a permission set's session duration does not affect credentials that were already issued. The new value applies the next time credentials are fetched, and an administrator may need to reprovision the permission set to the accounts for the change to take effect.
Edge cases
- Clock skew. If your machine's clock is wrong by more than a few minutes, the CLI may treat fresh credentials as expired or send requests that AWS rejects with signature errors. Fix the clock before debugging anything else.
- Several profiles, one session. Profiles that share one
sso-sessionshare one access token. Oneaws sso logincovers them all. Each profile still gets its own role credentials, with the duration of its own permission set. - Different
sso-sessionblocks. Each block has its own cached token, so each needs its own sign-in. - Role chaining. If a profile assumes another role with
role_arnandsource_profile, the chained credentials last at most 1 hour, whatever the permission set says.
Exercise
Your Identity Center session is 8 hours and your permission set's session duration is 12 hours. You sign in at 09:00 using a profile with an sso_session block. At 18:00 a command fails with an expired-session error. Why, and what should you do?
Show answer
The Identity Center session ended at 17:00, 8 hours after sign-in. The permission set duration only controls how long each set of role credentials lasts. It does not extend the session. The CLI can no longer refresh the access token or fetch new role credentials, so run aws sso login --profile <name> to start a new session.
Exercise
A teammate exports credentials with eval "$(aws configure export-credentials --profile dev --format env)" and starts a 3-hour job. The permission set duration is 1 hour. What happens, and give two ways to avoid the failure.
Show answer
The job fails with an expired token error about 1 hour after the export, because exported environment variables are never refreshed. To avoid it, run the job with the profile directly so the CLI or SDK refreshes the credentials, or use a credential_process profile that calls aws configure export-credentials --format process, which fetches new credentials when needed. An administrator could also raise the permission set duration to 4 hours or more, but this affects every user of that permission set.