Sub topic 6
Signing in from the terminal
aws sso login is the command that turns your IAM Identity Center configuration into a working session. This chapter covers what the command does, the flags that change its behavior, and the errors you are most likely to see.
This chapter assumes the following:
- AWS CLI version 2 is installed.
- You have an
sso-sessionblock and at least one profile in~/.aws/config, either written by hand or created byaws configure sso. - Your Identity Center user has a permission set assigned in the account the profile names.
What the command does
The command signs you in to Identity Center. It does not fetch role credentials. Those come later, when you run a command that needs them.
aws sso login --profile devThe CLI reads the dev profile, follows its sso_session = my-sso line to the [sso-session my-sso] block, and takes the start URL, region, and scopes from there. Then it does the following:
- It looks for a valid cached token for that session. If one exists and has not expired, it can finish without opening a browser.
- If there is no valid token, it registers itself as an OIDC client with Identity Center. The registration is cached and reused.
- It starts an authorization request and opens your default browser.
- You sign in with your Identity Center credentials, and MFA if your organization requires it. You then approve the request for the CLI.
- The CLI exchanges the approval for an access token and, when the session is configured with
sso_registration_scopes, a refresh token. It writes both to the cache.
The terminal prints a short confirmation such as Successfully logged into Start URL: https://example.awsapps.com/start.
Logging in by profile or by session
Both of these forms work:
aws sso login --profile dev
aws sso login --sso-session my-ssoThe token belongs to the sso-session, not to the profile. In the Topic's example config, dev and prod share my-sso. One login therefore covers both profiles, and either command above works for both.
Use --sso-session when you want to sign in without picking a profile. Use --profile when you are about to work in a specific account and want the CLI to check that the profile is configured correctly.
If you set neither flag, the CLI uses the profile from AWS_PROFILE, or default if that is unset. Chapter 8 covers profile selection in detail.
Profiles without an sso-session block
Older configurations put sso_start_url and sso_region directly in the profile. These "legacy" profiles still work with aws sso login. They do not get refresh tokens, so you sign in again whenever the access token expires. Moving to an sso-session block is the recommended setup and is what this tutorial uses.
Choosing how the browser step happens
The default flow opens a browser on the same machine and listens on a local port for the result. That fails in places where there is no local browser, such as an SSH session or a container. Two flags handle the other cases.
| Flag | Behavior | Use it when |
|---|---|---|
| (none) | Opens your default browser and receives the result on a local redirect | You are on a desktop with a browser |
| `--no-browser` | Prints the authorization URL instead of opening it | Your browser is not the default, or you want to paste the URL into a specific browser profile |
| `--use-device-code` | Uses the device authorization flow: the CLI prints a URL and a short code, and you enter the code in a browser on any device | You are on a remote host, a container, or a machine without a browser |
aws sso login --profile dev --use-device-codeWith the device flow, the CLI prints something like this:
Browser will not be automatically opened.
Please visit the following URL:
https://device.sso.us-east-1.amazonaws.com/
Then enter the code:
ABCD-EFGHYou can open that URL on your laptop while the CLI waits on the server. The CLI polls Identity Center until you approve, then writes the token on the machine where the CLI is running.
Confirming the login worked
A successful login message only tells you the token was cached. The real test is a call that needs role credentials:
aws sso login --profile dev
aws sts get-caller-identity --profile devThe output shows the account ID and an assumed-role ARN that contains the permission set's role name. The next chapter covers how to read it.
Where the result goes
The login writes JSON files under ~/.aws/sso/cache/. One file holds the registered client and one holds the token. The token file contains fields such as accessToken, expiresAt, and, for sso-session profiles, refreshToken. The file name is derived from a hash of the session name, so the CLI can find it again without storing your start URL in the name.
ls ~/.aws/sso/cache/Chapter 9 covers the cache layout in more detail.
When the CLI asks you to log in again
You do not need to run aws sso login before every command. You run it when the CLI cannot get a valid token on its own. With an sso-session profile, the CLI first tries to refresh the access token using the refresh token. It only falls back to asking you to sign in when that is not possible, for example when the Identity Center session has reached its maximum duration or an administrator has revoked it.
The login also does not prevent individual commands from failing later. The role credentials the CLI fetches with your token are short-lived, and the CLI fetches new ones automatically as long as the token is valid.
Common errors at this step
| Message (approximately) | Likely cause | Fix |
|---|---|---|
| `Missing the following required SSO configuration values: sso_start_url, sso_region` | A legacy-style profile is missing fields, or `sso_session` names a block that does not exist | Check that the `sso_session` value matches an `[sso-session NAME]` header exactly |
| `The config profile (dev) could not be found` | The profile name is misspelled or the profile is in a different config file | Run `aws configure list-profiles` and compare |
| `Error loading SSO Token: Token for https://... does not exist` | You have not logged in for this session, or you cleared the cache | Run `aws sso login` |
| `The SSO session associated with this profile has expired or is not valid` | The token expired and could not be refreshed | Run `aws sso login` again |
| Browser opens but the CLI never finishes | Redirect cannot reach the CLI (remote host, firewall, wrong browser profile) | Use `--use-device-code` |
| The browser page shows no accounts or roles | Your user has no permission set assigned, or you signed in as a different Identity Center user | Ask an administrator to check your assignments, then sign out of the portal and retry |
Chapter 12 returns to these errors with a fuller diagnostic procedure.
A practical routine
For daily work, a short routine covers most cases:
# Start of the day, or when a command reports an expired session
aws sso login --sso-session my-sso
# Make the shell use one profile for this session
export AWS_PROFILE=dev
# Confirm the identity before you run anything that changes state
aws sts get-caller-identityRunning the identity check before any state-changing command takes a second and prevents mistakes such as running a change against prod because AWS_PROFILE was set in an earlier terminal tab.
Exercise
You are connected to a build server over SSH. You run aws sso login --profile dev --no-browser, open the printed URL in the browser on your laptop, and approve the request, but the command on the server never finishes. What went wrong, and which command should you run instead?
Show answer
With --no-browser the CLI still expects the authorization result to be sent back to a local redirect on the machine running the CLI. Your approval happened in a browser on a different machine, so the redirect never reached the server. Run aws sso login --profile dev --use-device-code. This uses the device authorization flow: the CLI prints a URL and a code, you enter the code in any browser, and the CLI polls Identity Center until the approval arrives.
Exercise
Your config has profiles dev and prod that both set sso_session = my-sso. You run aws sso login --profile dev. Do you need to run another login before using --profile prod? Why or why not?
Show answer
No. The token is cached per sso-session, not per profile, and both profiles use my-sso. The CLI uses the same access token to request role credentials for each profile's account and role. You only need a new login if the token cannot be refreshed or has expired.