Sub topic 7
Checking who you are with sts get-caller-identity
This chapter assumes you have an sso-session and a profile named dev in ~/.aws/config, and that you can run aws sso login --profile dev. It also assumes you know what an IAM role and an AWS account ID are. Here you learn to read the output of aws sts get-caller-identity and use it to catch wrong-account and wrong-role mistakes before they cost you anything.
Why check identity first
Signing in succeeds even when you picked the wrong profile. A successful aws sso login only tells you that the browser flow worked. It does not tell you which account or role your next command will use. Many profiles, a leftover environment variable, or a shell that still has AWS_PROFILE set from yesterday can all send a command to the wrong place.
aws sts get-caller-identity answers one question: which principal do these credentials represent right now? It is the first command to run after sign-in, and the first to run when something behaves strangely.
Two properties make it a good check:
- It needs no IAM permissions. Any valid credentials can call it, and an explicit
Denyonsts:GetCallerIdentitydoes not block it. A failure therefore points at the credentials, not at a policy. - It changes nothing. You can run it as often as you like.
Running the command
aws sso login --profile dev
aws sts get-caller-identity --profile devA typical response for an Identity Center user looks like this:
{
"UserId": "AROAEXAMPLEROLEID1234:[email protected]",
"Account": "111122223333",
"Arn": "arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAccess_0123456789abcdef/[email protected]"
}The call is made with the temporary role credentials the CLI fetched for the dev profile, so the output describes that role session, not your Identity Center user directly.
Reading the three fields
| Field | What it holds | What to check |
|---|---|---|
| `Account` | The 12-digit account ID of the credentials | It matches the `sso_account_id` of the profile you meant to use |
| `Arn` | The ARN of the assumed-role session | It names the permission set you expect |
| `UserId` | The unique role ID, a colon, then the session name | The part after the colon is your Identity Center username |
Taking the ARN apart
The ARN has a fixed shape for Identity Center sign-ins:
arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAccess_0123456789abcdef/[email protected]stsandassumed-roletell you these are temporary credentials from a role, not an IAM user. If you seeuser/instead, you are using long-term access keys.111122223333is the account.AWSReservedSSO_ReadOnlyAccess_0123456789abcdefis the role. Identity Center creates it in each assigned account. The format is the prefixAWSReservedSSO_, then the permission set name, then a generated suffix.[email protected]is the role session name, which is your Identity Center username.
The role itself lives under the path /aws-reserved/sso.amazonaws.com/. The get-caller-identity ARN omits the path. You will see the full path if you look up the role in IAM or in a policy's Principal element.
Extracting one field for scripts
For a quick look, --query and --output text return a single value:
aws sts get-caller-identity --profile dev --query Account --output text
aws sts get-caller-identity --profile dev --query Arn --output textA small guard stops a script before it touches the wrong account:
#!/usr/bin/env bash
set -euo pipefail
expected_account="111122223333"
actual_account="$(aws sts get-caller-identity --query Account --output text)"
if [[ "$actual_account" != "$expected_account" ]]; then
echo "Wrong account: expected $expected_account, got $actual_account" >&2
exit 1
fi
echo "Running in account $actual_account"Because the guard has no --profile flag, it checks whatever the shell would use for every other command. That is the point: the guard sees what later commands will see.
Comparing two profiles
With the dev and prod profiles from the earlier chapter, run the check against each one. One sign-in covers both because they share an sso-session.
for p in dev prod; do
echo "== $p"
aws sts get-caller-identity --profile "$p" --query '[Account,Arn]' --output text
doneYou should see two different account IDs and, in this setup, the same permission set name in both role ARNs. The role suffix differs per account. If both lines show the same account, check the sso_account_id values in your config.
When the answer is not what you expected
The same command also tells you which source of credentials won. The CLI resolves credentials in a fixed order, and a stale source can override the one you intended.
Environment variables beat the profile from AWS_PROFILE
If AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set in your shell, they take precedence over a profile chosen through AWS_PROFILE. The result is that get-caller-identity shows an old IAM user or a role from an earlier export-credentials run, not your SSO role. Passing --profile on the command line overrides them.
Check for leftovers:
env | grep '^AWS_' | sed 's/=.*/=.../'The sed hides the values so secrets do not end up in your terminal history or screenshots. Clear anything unexpected:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKENTo see which source the CLI picked, use:
aws configure list --profile devThe Type column reports where each value came from, such as sso, env, or config-file.
A user ARN instead of an assumed role
An ARN ending in :user/some-name means long-term access keys. Those may sit in ~/.aws/credentials, in the environment, or in a profile without sso_session. This is the situation the tutorial is moving you away from, so treat it as a finding to fix.
The wrong permission set
If the role name says AdministratorAccess when you expected ReadOnlyAccess, the profile's sso_role_name is wrong, or you are using a different profile than you thought. The permission sets you may choose from are those assigned to you in that account, so the fix is in your config, not in IAM.
Common failures of the command itself
If the command fails, the credentials are the problem, because the call needs no permissions. The usual messages:
| Message (abbreviated) | Meaning | Fix |
|---|---|---|
| `The SSO session associated with this profile has expired or is not valid` | The cached sign-in token expired or was removed | Run `aws sso login --profile dev` |
| `Error when retrieving token from sso: Token has expired and refresh failed` | The same condition, reported by the token provider | Run `aws sso login --profile dev` |
| `The config profile (dev) could not be found` | The profile name is misspelled or missing from the config file | Check `~/.aws/config` for `[profile dev]` |
| `Unable to locate credentials` | No source in the chain produced credentials | Set `--profile` or `AWS_PROFILE`, or sign in |
The first two mean the same thing in practice: sign in again. Later chapters cover how long each token lasts and how to diagnose more complex cases.
A habit to build
- Sign in with
aws sso login --profile <name>. - Run
aws sts get-caller-identity --profile <name>. - Confirm the account and the permission set in the ARN.
- Only then run commands that change anything.
Repeat step 2 whenever you switch terminals, change profile, or see behavior that does not match what you expect.
Exercise
You run aws sts get-caller-identity with no flags and get an ARN ending in :user/ci-deploy, although you signed in with aws sso login --profile dev and exported AWS_PROFILE=dev. What is the most likely cause, and how do you confirm and fix it?
Show answer
Long-term access keys for the ci-deploy IAM user are set in the environment (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY), and they take precedence over the profile chosen by AWS_PROFILE. Confirm with env | grep '^AWS_' or aws configure list, whose Type column shows env. Fix it by running unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN, or pass --profile dev explicitly, then rerun the check and confirm the ARN contains assumed-role/AWSReservedSSO_.
Exercise
Break down this ARN: arn:aws:sts::444455556666:assumed-role/AWSReservedSSO_PowerUserAccess_fedcba9876543210/[email protected]. Which account, which permission set, and which user does it show, and why can get-caller-identity still work if an IAM policy denies every STS action?
Show answer
The account is 444455556666, the permission set is PowerUserAccess, and the user (role session name) is [email protected]. sts:GetCallerIdentity needs no permissions, and an explicit deny on it does not block the call, so it works whenever the credentials are valid.