Sub topic 11
Signing in to the CLI with IAM Identity Center
This chapter assumes you have installed AWS CLI v2, you know what a named profile is, and your organization has enabled IAM Identity Center (the successor to AWS Single Sign-On). You also need a user in Identity Center with at least one permission set assigned to an account.
Why Identity Center instead of access keys
A long-lived IAM user access key is a secret that works until someone deletes it. If it leaks, an attacker keeps access for as long as the key exists. IAM Identity Center avoids this. You sign in through a browser with your normal workforce identity, including MFA if your organization requires it. The CLI then receives short-lived credentials for one account and one role.
Three terms appear throughout this chapter:
- Start URL: the address of your AWS access portal, for example
https://my-sso-portal.awsapps.com/start. - Permission set: a template of permissions that Identity Center turns into an IAM role in each account you are assigned to. The CLI calls this value
sso_role_name. - SSO session: a named block of configuration holding the start URL, the Region where Identity Center lives, and the scopes the CLI requests. Several profiles can share one session, so you sign in once for all of them.
What `aws configure sso` writes
Run the wizard once per profile:
aws configure sso
aws sso login --profile dev
aws sts get-caller-identity --profile devThe wizard asks for a session name, the start URL, the Identity Center Region, and the registration scopes. Accept the default scope sso:account:access unless you have a reason to change it. It then opens a browser so you can authorize the CLI. After that, it lists the accounts and roles you can use, and it asks for a default Region, an output format, and a profile name. The result is written to ~/.aws/config:
[default]
region = us-east-1
output = json
[profile dev]
region = eu-west-1
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = ReadOnly
[sso-session my-sso]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = us-east-1
sso_registration_scopes = sso:account:accessNote the two Regions. sso_region is where Identity Center is hosted. The profile's region is where your commands run by default. They are often different, and mixing them up is a common mistake.
No secret appears in this file. The account ID, role name, and start URL are identifiers, not credentials.
Signing in and checking who you are
aws sso login --profile dev opens your browser, and you approve the request there. If the CLI cannot open a browser, as on a remote server, add --no-browser. The CLI then prints a URL and a code to enter on any device.
Always verify the result:
aws sts get-caller-identity --profile devThe output shows UserId, Account, and Arn. For an Identity Center profile, the ARN looks like arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnly_abc123/your.name. Check that the account ID and role name match what you expect before you run anything that changes resources.
How the sign-in works internally
Two separate lifetimes matter:
- The SSO access token is cached under
~/.aws/sso/cache. It lasts as long as the session duration your administrator configured, from one hour up to 90 days. - The role credentials are fetched from Identity Center when needed and are short-lived, with a duration set by the permission set (the default is 1 hour). With an
sso-sessionblock, the CLI refreshes them for you while the token is valid.
When the token expires, commands fail until you run aws sso login again.
Sharing one session across profiles
Because the sso-session block is separate, you can add more profiles that point at the same session. Each profile names a different account or role:
[profile prod-readonly]
region = us-east-1
sso_session = my-sso
sso_account_id = 444455556666
sso_role_name = ReadOnlyOne aws sso login --sso-session my-sso then signs you in for dev and prod-readonly together. Select a profile per command with --profile, or for the whole shell:
export AWS_PROFILE=prod-readonly
aws sts get-caller-identitySigning out
aws sso logout deletes the cached tokens and clears cached role credentials. Run it on shared machines. It does not end your browser session with the access portal, so the next aws sso login may complete without prompting you for a password.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Error loading SSO Token: Token for ... does not exist` | You have not signed in for this session | Run `aws sso login --profile NAME` |
| `The SSO session associated with this profile has expired or is otherwise invalid` | The cached token expired | Run `aws sso login` again |
| `Unable to locate credentials` | The profile name is misspelled or `AWS_PROFILE` points to a profile that does not exist | Check `aws configure list-profiles` |
| `AccessDenied` on a command | You signed in correctly, but the permission set does not allow the action | Ask an administrator for a broader permission set, or use a different role |
| Wrong account in the ARN | A different profile or `AWS_PROFILE` is active | Pass `--profile` explicitly and re-run `get-caller-identity` |
| Login works but resources are missing | The profile `region` is not the Region holding your resources | Pass `--region` or fix the profile |
Practical habits
- Use a read-only permission set for exploration and switch to a write role only for changes.
- Name profiles after the account and role, such as
prod-readonly, so the command line shows what you are about to touch. - Do not copy the contents of
~/.aws/sso/cacheto other machines. Those files are bearer tokens. - If you need credentials for a tool that does not understand SSO profiles, run
aws configure export-credentials --profile dev --format env. It prints short-lived values you can use in that shell, and they expire on their own.
Exercise
You run aws s3 ls --profile dev and get "The SSO session associated with this profile has expired or is otherwise invalid." Which command fixes it, and which command then confirms you are in the right account and role?
Show answer
Run aws sso login --profile dev to get a new access token. Then run aws sts get-caller-identity --profile dev and check that the account ID and the role name inside the assumed-role/AWSReservedSSO_... ARN match what you expect.
Exercise
A profile has sso_region = us-east-1 and region = eu-west-1. A colleague says one of them is wrong because they differ. Are they right?
Show answer
No. sso_region is where IAM Identity Center is hosted and is used for sign-in. region is the default Region for service commands. They are separate settings and can differ.