Sub topic 16
Using assigned accounts from the command line
This chapter assumes you know how to run AWS CLI commands, understand IAM roles and temporary credentials, and have already assigned a permission set to a group in at least one account. Here you will use those assignments from a terminal. You will configure profiles, sign in, run commands, and handle the errors you are most likely to hit.
How CLI access relates to provisioning
When you assign a permission set to an account, IAM Identity Center provisions a role in that account. Its name starts with AWSReservedSSO_ and includes the permission set name. People never assume that role through a normal IAM trust relationship with a long-lived key. They sign in to Identity Center, and the AWS access portal then issues temporary credentials for that role.
The AWS CLI (version 2) automates this flow. A profile names one account and one permission set. The CLI uses a cached sign-in token to ask the portal for role credentials whenever it needs them.
There are two separate lifetimes:
- The portal session (the access token) is set in the Identity Center settings. Its default is 8 hours.
- The role session is the permission set's session duration, such as the
PT4Hset in earlier chapters.
When the access token expires, you must run aws sso login again. When the role credentials expire, the CLI fetches new ones silently as long as the token is still valid.
Configuring an sso-session profile
The recommended format uses a shared sso-session section. Several profiles can reuse one session, so you sign in once for all of them.
[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:accessEach setting does one job:
| Setting | Meaning |
|---|---|
| `sso_start_url` | The AWS access portal URL of your Identity Center instance. |
| `sso_region` | The Region where Identity Center is enabled. It can differ from the profile's `region`. |
| `sso_registration_scopes` | The OAuth scope the CLI requests. `sso:account:access` is enough for role credentials. |
| `sso_account_id` | The 12-digit target account. |
| `sso_role_name` | The permission set name, not the full `AWSReservedSSO_...` role name. |
| `region` | The Region where your service calls go by default. |
You can also let the CLI write this file for you:
aws configure ssoIt prompts for the session name, start URL, and scopes. It then opens a browser, lists the accounts and permission sets you can use, and saves the profile.
Signing in and running commands
Sign in once per session name, then use any profile that references it:
aws sso login --profile audit-prod
aws sts get-caller-identity --profile audit-prodThe second command shows which role the CLI is using. The Arn contains the reserved role name and your session name, which is normally your username:
arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAuditor_0123456789abcdef/[email protected]To avoid repeating --profile, set an environment variable for the shell:
export AWS_PROFILE=audit-prod
aws s3 lsTo end the session and clear cached tokens, run:
aws sso logoutFinding what you can access
If you do not know the account IDs, query the portal directly. These calls use the cached access token, so you must be signed in. The aws sso commands take the token as a parameter, so the easier route is aws configure sso, which lists accounts for you. After sign-in you can inspect the cache:
ls ~/.aws/sso/cache/Treat these cache files as secrets. They contain tokens that can be exchanged for credentials.
Many profiles for many accounts
With dozens of accounts, writing profiles by hand is slow. Because every profile follows the same pattern, you can generate them. The script below reads accounts under one OU from Organizations (run it with management or delegated administrator credentials) and appends a profile for each one.
#!/usr/bin/env bash
set -euo pipefail
OU_ID="ou-xxxx-xxxxxxxx"
ROLE="ReadOnlyAuditor"
SESSION="corp"
REGION="us-east-1"
for ACCOUNT_ID in $(aws organizations list-accounts-for-parent \
--parent-id "$OU_ID" \
--query 'Accounts[?Status==`ACTIVE`].Id' --output text); do
NAME=$(aws organizations describe-account \
--account-id "$ACCOUNT_ID" \
--query 'Account.Name' --output text | tr ' ' '-' | tr 'A-Z' 'a-z')
cat >> ~/.aws/config <<EOF
[profile ${NAME}-${ROLE}]
sso_session = ${SESSION}
sso_account_id = ${ACCOUNT_ID}
sso_role_name = ${ROLE}
region = ${REGION}
EOF
doneRun it once and review the result. Running it again appends duplicate profiles, so remove old entries first.
Using credentials with tools that lack SSO support
Some older SDKs and tools do not understand sso-session profiles. The CLI can export short-lived credentials as environment variables:
eval "$(aws configure export-credentials --profile audit-prod --format env)"This sets AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN for the current shell. The credentials expire with the role session, so you must repeat the command later. A credential process entry is also possible, but the export approach is simpler for one-off use.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `The SSO session associated with this profile has expired or is otherwise invalid` | The access token expired or was revoked. | Run `aws sso login --profile NAME`. |
| `Error loading SSO Token: Token for corp does not exist` | You never signed in for that session name. | Run `aws sso login` with a profile that uses the session. |
| `No access` or `ForbiddenException` when getting role credentials | The user has no assignment for that account and permission set, or the name in `sso_role_name` is wrong. | Check the assignment and the exact permission set name. |
| The profile works but the call returns `AccessDenied` | The permission set's policies do not allow the action, or a permissions boundary or SCP blocks it. | Review the permission set policies, then Organizations SCPs. |
| A new assignment does not appear in the portal | Provisioning has not finished or failed. | Check provisioning status and re-provision if needed. |
| Wrong Region errors on every call | `sso_region` is correct but `region` is missing or points elsewhere. | Set `region` in the profile. |
A frequent mistake is mixing up the two Regions. sso_region identifies where Identity Center lives. region identifies where your service calls go. They are often the same, but they do not have to be.
Checking access from the server side
If a user says they cannot sign in to an account, check from the administrator side:
- List the user's group memberships in the identity store.
- List the assignments for the account with
aws sso-admin list-account-assignments. - Confirm the permission set is provisioned there with
aws sso-admin list-permission-sets-provisioned-to-account. - In the target account, confirm the
AWSReservedSSO_role exists. - Search CloudTrail for the user's sign-in and role-credential events if the role exists but access still fails.
An example for step 3:
aws sso-admin list-permission-sets-provisioned-to-account \
--instance-arn "$INSTANCE_ARN" \
--account-id 111122223333If your permission set is missing from the output, the assignment either was not created or has not been provisioned. Re-run the provisioning call from an earlier chapter.
Exercise
A teammate runs aws s3 ls --profile audit-prod and sees "The SSO session associated with this profile has expired or is otherwise invalid." Another profile, audit-dev, uses the same sso_session = corp and worked an hour ago. What should the teammate do, and why will it likely fix both profiles?
Show answer
Run aws sso login --profile audit-prod. The error means the cached access token for the session corp has expired. Both profiles reference the same sso-session section, so they share one token. A single login renews it, and role credentials for either profile are then fetched again automatically.
Exercise
A profile has sso_role_name = AWSReservedSSO_ReadOnlyAuditor_0123456789abcdef and fails with a ForbiddenException. What is wrong?
Show answer
The profile must use the permission set name, ReadOnlyAuditor, not the full reserved role name that appears in the target account. The CLI asks the portal for the permission set by name.