Sub topic 7
Checking who you are with sts get-caller-identity
This chapter assumes you have AWS CLI version 2 installed, an sso-session and at least one profile in ~/.aws/config, and a working aws sso login. If you have not signed in yet, do that first.
After you sign in, the CLI holds a token. That token says nothing about which account or role a later command will use. The command aws sts get-caller-identity answers that question. It needs no IAM permissions, so it works with any valid credentials.
Running the check
Sign in, then ask AWS who you are:
aws sso login --profile dev
aws sts get-caller-identity --profile devThe output is JSON with three fields:
{
"UserId": "AROAEXAMPLEID:[email protected]",
"Account": "111122223333",
"Arn": "arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAccess_0123456789abcdef/[email protected]"
}Reading the output
| Field | What it tells you |
|---|---|
| `Account` | The 12-digit account the credentials act in. Compare it with `sso_account_id` in the profile. |
| `Arn` | The identity AWS sees. It is an `assumed-role` ARN, not an IAM user. |
| `UserId` | The role's unique ID, a colon, then the session name. For Identity Center this is your user name. |
The role name in the ARN starts with AWSReservedSSO_, followed by the permission set name (ReadOnlyAccess here) and a generated suffix. Identity Center creates this role in each assigned account. If you see user/ instead of assumed-role/, the CLI is using long-term access keys from somewhere else, not your SSO profile.
Useful variations
Use --query to print one field, which is handy in scripts:
aws sts get-caller-identity --profile prod --query Account --output textChecking both profiles confirms they point at different accounts:
for p in dev prod; do
echo "$p: $(aws sts get-caller-identity --profile "$p" --query Arn --output text)"
doneWhen the check fails
The error text points to the cause.
Error loading SSO Token: Token for ... does not existorThe SSO session associated with this profile has expired or is otherwise invalid: runaws sso login --profile devagain.The config profile (dev) could not be found: the profile name is misspelled, or the[profile dev]header is missing in~/.aws/config.Missing the following required SSO configuration values: the profile lackssso_session,sso_account_idorsso_role_name.- An unexpected account or an
IAM userARN: another source is winning the credential search. Check whetherAWS_ACCESS_KEY_IDis set withenv | grep AWS_, because environment variables take precedence over a profile chosen byAWS_PROFILE.
Exercise
You run aws sts get-caller-identity --profile dev and the Arn contains user/ci-deploy instead of assumed-role/AWSReservedSSO_.... What is the likely cause, and how do you find it?
Show answer
The CLI is using long-term access keys for an IAM user, not the SSO profile. Environment variables such as AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY take precedence over the profile. Run env | grep AWS_ to find them and unset them, then run the command again.