Sub topic 16
Using assigned accounts from the command line
This chapter assumes you know the AWS CLI config file, and that you have a permission set assigned to a group you belong to in at least one account. You will sign in with IAM Identity Center and run commands against an assigned account without long-lived access keys.
How CLI sign-in works
The CLI signs you in once, through your browser. It then exchanges that sign-in for short-lived credentials for one account and one permission set. Those credentials belong to the role that Identity Center provisioned in the account. Its name starts with AWSReservedSSO_.
Configure a profile
Add a profile that names the account and the permission set. Put the portal settings in a shared sso-session block.
[profile audit-prod]
sso_session = corp
sso_account_id = 111122223333
sso_role_name = ReadOnlyAuditor
region = us-east-1
[sso-session corp]
sso_start_url = https://example.awsapps.com/start
sso_region = us-east-1
sso_registration_scopes = sso:account:accesssso_region is the Region where Identity Center is enabled. The profile's region is where your commands run by default. They can differ.
Running aws configure sso writes these blocks for you. It lists the accounts and permission sets assigned to you and asks which to use.
Sign in and verify
aws sso login --profile audit-prod
aws sts get-caller-identity --profile audit-prodThe second command should return an ARN like arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAuditor_0123456789abcdef/alice. If the role name matches your permission set, the setup works.
Profiles that share the same sso_session share one sign-in. Run aws sso login --sso-session corp once, and every profile that uses corp works until the session expires. On a machine with no browser, add --use-device-code.
Two lifetimes
Two timers apply:
- The permission set session duration controls how long the role credentials last. The earlier example set
PT4H. - The sign-in session controls how long the CLI can fetch new credentials without a browser prompt.
When either one expires, you get an error such as "The SSO session associated with this profile has expired or is not valid." Run aws sso login again. aws sso logout clears the cached token and credentials.
Common errors
| Symptom | Likely cause |
|---|---|
| The account or role is missing from `aws configure sso` | No assignment exists for you or your group. Check the group membership and the assignment. |
| `sts get-caller-identity` shows the wrong role | `sso_role_name` or `sso_account_id` in the profile is wrong. |
| `AccessDenied` on a command | The permission set's policies don't allow the action. After a policy change, re-provision the permission set. |
| Login succeeds but commands fail with an expired token | The role credentials outlived the session duration. Log in again. |
Exercise
You have two profiles, audit-prod and audit-dev, that both use sso_session = corp. How many browser sign-ins do you need to use both, and which timer decides when the role credentials stop working?
Show answer
One browser sign-in is enough, for example aws sso login --sso-session corp, because both profiles share the session. The permission set's session duration decides when the role credentials stop working. The CLI then fetches new ones while the sign-in session is still valid.