Sub topic 6
Signing in from the terminal
This chapter assumes you have AWS CLI version 2 installed and an sso-session block with at least one profile in ~/.aws/config, either written by hand or created by aws configure sso. You should also know what a permission set is. Here you will sign in, see what the CLI does during sign-in, and learn what the CLI stores afterward.
What `aws sso login` does
aws sso login authorizes the CLI to act for your Identity Center user. It does not return role credentials directly. It obtains an access token for your sign-in session. The CLI later exchanges that token for short-lived role credentials whenever a command needs them.
aws sso login --profile devThe dev profile points at an sso_session, so the login applies to the whole session. Every profile that names the same sso_session can then use it. With the two-profile configuration from the previous chapter, signing in once covers dev and prod.
The device authorization flow
The CLI opens your default browser at a verification page and prints a URL and a code. You approve the request in the browser. Meanwhile, the CLI polls the service until the approval arrives.
Check that the code shown in the browser matches the code in your terminal before you approve. The code ties the browser approval to the request your terminal started.
If the CLI cannot open a browser, for example over SSH, add the --no-browser option. The CLI then prints the URL and code, and you open them on any device.
aws sso login --profile dev --no-browserConfirming the sign-in worked
The login command only proves that you have a token. To prove that role credentials work, run a command that uses them:
aws sts get-caller-identity --profile devThe output shows the account ID and an assumed-role ARN for the role that Identity Center created from your permission set. The next chapter covers reading that output in detail.
What the CLI stores
After sign-in, the CLI writes the access token to a JSON file under ~/.aws/sso/cache/. When you run a command with an SSO profile, the CLI uses the token to request role credentials for the profile's account and role, and it caches them too. You do not have to sign in again for each command until the cached values expire.
| Item | Obtained by | Typical use |
|---|---|---|
| Access token | `aws sso login` | Proves your Identity Center sign-in |
| Role credentials | Automatic, from the token | Signing each AWS API request |
Treat the cache directory like a credentials file. Anyone who can read the token can obtain role credentials until it expires.
Common mistakes
- Running
aws sso loginwith no--profileand noAWS_PROFILE. The CLI cannot tell which session to use. Pass--profile, or use--sso-session my-ssoto name the session directly. - Signing in with one profile and then running commands with another that uses a different
sso_session. The second session has no token. - Approving the browser prompt in a different Identity Center portal than the one named by
sso_start_url.
Exercise
You configured the dev and prod profiles with the same sso_session. You ran aws sso login --profile dev. Do you need to sign in again before running aws s3 ls --profile prod? Why or why not?
Show answer
No. Both profiles share one sso-session, so the access token from the login applies to both. The CLI uses the cached token to request role credentials for the prod account and role. You would need a new login only if the token had expired or you ran aws sso logout.
Exercise
You are connected to a remote server over SSH and aws sso login --profile dev cannot open a browser. What option do you add, and what do you do next?
Show answer
Add --no-browser. The CLI prints a verification URL and a code. Open the URL in a browser on any device, check that the code matches, and approve the request. The CLI then receives the token.