Sub topic 11
Signing in to the CLI with IAM Identity Center
This chapter assumes you have installed AWS CLI v2 and understand named profiles in ~/.aws/config. You also need an IAM Identity Center user with access to at least one account and permission set. Your administrator provides the start URL and the Region of the portal.
Why use Identity Center for the CLI
IAM Identity Center signs you in through your browser with your workforce identity. The CLI then receives short-lived credentials for one account and one role. You never create or store a long-lived access key, and the credentials expire on their own.
The configuration pieces
An SSO setup has two parts in ~/.aws/config:
- An
sso-sessionsection holds the portal start URL, the portal Region, and the registration scopes. Several profiles can share one session. - A profile section names the session, the account ID, and the permission set (shown as
sso_role_name).
[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 that sso_region is where Identity Center lives. The profile's region is where your commands run by default. They can differ.
Create the profile and sign in
Run the interactive setup once. It asks for a session name, the start URL, and the portal Region. It then opens your browser, lists the accounts and roles you can use, and writes the profile for you. Next, sign in and check the result.
aws configure sso
aws sso login --profile dev
aws sts get-caller-identity --profile devThe get-caller-identity output shows the account ID and an ARN containing the role name. If these match your profile, the sign-in worked.
Session lifetime
The token is cached under ~/.aws/sso/cache. The CLI uses it to fetch fresh role credentials whenever a command needs them. When the session expires, commands fail with a message such as "The SSO session associated with this profile has expired or is not valid". Run aws sso login --profile dev again. The session length is set by your administrator.
To end a session early, run aws sso logout. This removes the cached tokens and the role credentials derived from them.
Common mistakes
- Running a command without
--profile devwhile no default credentials exist. SetAWS_PROFILE=devin your shell to avoid repeating the flag. - Confusing
sso_regionwith the profileregion. A wrongsso_regionmakes login fail. A wrongregionsends commands to the wrong place. - Expecting
aws configureto set up SSO. Useaws configure ssoinstead.
Exercise
Exercise
You run aws s3 ls --profile dev and get an error saying the SSO session has expired. What command fixes it, and why do you not need to run aws configure sso again?
Show answer
Run aws sso login --profile dev. The profile and sso-session sections already exist in ~/.aws/config, so only the cached token needs renewing. aws configure sso is for creating the configuration the first time.