Skip to content
Visual explainer 2h 30m to learn 5 steps

AWS CLI profiles, temporary credentials and caller identity

Sign in to the CLI with IAM Identity Center, confirm who and where you are with STS, and explain how SDKs find credentials.

By Invicly Tech, , in AWS Cloud Engineering. About 12 minutes to read.

Topic 3 of 38

In short

The AWS CLI can sign in through IAM Identity Center and receive temporary credentials instead of storing long-lived access keys. aws sts get-caller-identity tells you which account and role you are acting as, and the Region comes from a flag, an environment variable or the profile. SDKs and the CLI search for credentials in a fixed order, so knowing that order explains why a command uses the identity it does.

Think of it like this: An SSO profile is like a day pass from reception: you show your badge once, get a pass that expires tonight, and anyone can read the pass to see who you are.

Explained simply

The AWS CLI is a command-line tool that turns commands like aws s3 ls into signed requests to AWS. To sign a request it needs credentials: an access key ID, a secret key and, for temporary credentials, a session token. The question is where those come from.

The safest answer for people is an SSO profile. You run aws configure sso once and answer a few questions. After that, aws sso login opens your browser, you sign in to IAM Identity Center with MFA, and the CLI receives credentials that expire after a few hours. No permanent secret is saved.

Because you might have several profiles and accounts, always check who you are before doing anything big. aws sts get-caller-identity prints the account number and the role you are using. It needs no permissions, so it works even when everything else is denied.

Every command also needs a Region. You can pass --region, set the AWS_REGION environment variable, or store a region in the profile. The flag wins over the variable, and the variable wins over the profile.

AWS SDKs for Python, JavaScript and other languages follow a similar search order to the CLI. They look at environment variables, then profiles and SSO, then container or instance roles. If code uses the wrong identity, walk that list from the top.

Press Play on a diagram to walk through it one step at a time.

Sequenceaws sso login and the first command
You and the CLI
Browser
IAM Identity Center
AWS STS and services
  1. 1aws sso login opens a browser sign-in page
    You and the CLI to Browser: aws sso login opens a browser sign-in page
  2. 2Sign in with password and MFA, approve the request
    Browser to IAM Identity Center: Sign in with password and MFA, approve the request
  3. 3SSO access token cached in ~/.aws/sso/cache
    IAM Identity Center to You and the CLI: SSO access token cached in ~/.aws/sso/cache
  4. 4Exchange token for role credentials (account + permission set)
    You and the CLI to IAM Identity Center: Exchange token for role credentials (account + permission set)
  5. 5Temporary key, secret and session token
    IAM Identity Center to You and the CLI: Temporary key, secret and session token
  6. 6Signed request: sts get-caller-identity
    You and the CLI to AWS STS and services: Signed request: sts get-caller-identity
  7. 7Account, assumed-role ARN, user ID
    AWS STS and services to You and the CLI: Account, assumed-role ARN, user ID
LayersPieces of a temporary credential
  1. 1
    Access key ID

    Starts with ASIA for temporary credentials

  2. 2
    Secret access key

    Used to sign requests, never sent

  3. 3
    Session token

    Proves the credentials came from STS

  4. 4
    Expiration

    After this time every request fails

In depth

What aws configure sso writes

aws configure sso asks for a session name, the start URL of your access portal (for example https://d-1234567890.awsapps.com/start), the Region where Identity Center lives and the registration scopes, then lets you pick an account and a role. It writes two sections to ~/.aws/config: an sso-session section with the start URL and SSO Region, and a profile section that points to that session and names the account ID, role name, default Region and output format.

No secret key is written. When you run aws sso login, the CLI caches an access token under ~/.aws/sso/cache. When a command runs, the CLI uses that token to fetch role credentials and caches them under ~/.aws/cli/cache until they expire. Several profiles can share one sso-session, so one sign-in covers every account and role you have been assigned.

  • sso-session: start URL, SSO Region, scopes
  • profile: sso_session, sso_account_id, sso_role_name, region
  • Token cache: ~/.aws/sso/cache
  • aws sso logout clears cached tokens

Reading get-caller-identity output

The response has three fields. Account is the 12-digit account ID. Arn is the identity that signed the request. For a role session it looks like arn:aws:sts::111122223333:assumed-role/RoleName/SessionName, and for Identity Center the role name starts with AWSReservedSSO_ followed by the permission set name and a suffix. For an IAM user it is arn:aws:iam::111122223333:user/name. UserId is the unique ID; for roles it combines the role ID with the session name.

Use it as a guard in scripts: compare Account with the expected sandbox ID and stop if it differs. That one check prevents the classic mistake of running a cleanup script against the wrong account. Because the call needs no IAM permission, it is also the first test when you suspect credentials are expired or missing: an error here means no valid credentials at all.

The credential provider chain

The CLI and SDKs stop at the first source that yields credentials. Command-line options come first (for the CLI, --profile chooses which profile to read). Then environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN. Then web identity settings, used by CI and Kubernetes. Then the selected profile in the shared config and credentials files, which can be static keys, an assume-role chain, SSO or an external process. Last come the container credentials endpoint used by ECS and the EC2 instance metadata service.

The practical rule: environment variables quietly beat your profile. If get-caller-identity shows an unexpected identity, run env | grep AWS_ and unset stale variables. On EC2 or ECS, leave the files and variables empty so the SDK falls through to the attached role.

FlowWhere the CLI looks for credentials, in order
  1. 1
    Command options

    --profile selects a profile

  2. 2
    Environment variables

    AWS_ACCESS_KEY_ID and friends

  3. 3
    Web identity

    AWS_ROLE_ARN plus a token file

  4. 4
    Config and credentials files

    SSO, assume role, process or keys

  5. 5
    Container credentials

    ECS task role endpoint

  6. 6
    Instance metadata

    EC2 instance profile via IMDS

Region selection and why long-lived keys are the last resort

Region resolution in CLI v2 runs from the --region flag, to the AWS_REGION variable, to AWS_DEFAULT_REGION, to the region setting in the profile. Most SDKs read AWS_REGION and the profile; some older tools only read AWS_DEFAULT_REGION, so setting the profile region is the most portable choice. aws configure list shows each value and whether it came from the environment, a file or the command line.

Long-lived access keys never expire on their own. They end up in shell history, dotfiles and screenshots, and attackers scan public code for them. For people, use SSO. For workloads on AWS, use attached roles. For CI outside AWS, use OIDC federation. If you truly must have a key, scope it narrowly, rotate it and alert on its use.

CompareLong-lived keys versus SSO credentials
IAM user access keys
  • Valid until someone deletes them
  • Stored in plain text in ~/.aws/credentials
  • No MFA at use time
  • Easy to leak in commits
SSO profile credentials
  • Expire after the session duration
  • Fetched on demand, cached briefly
  • Sign-in includes MFA
  • Revoked by removing the assignment

Learn it in 5 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Configure an SSO profile~30m
  2. 2
    Run sts get-caller-identity~30m
  3. 3
    Set and override the Region~30m
  4. 4
    Follow the credential provider chain~30m
  5. 5
    Avoid long-lived access keys~30m

Key points

  • aws configure sso writes a profile and an sso-session section into ~/.aws/config; no secret key is saved.
  • aws sts get-caller-identity needs no permissions and is the fastest way to answer: who am I, in which account?
  • Region precedence in CLI v2: --region flag, then AWS_REGION, then AWS_DEFAULT_REGION, then the profile's region setting.
  • Environment variables such as AWS_ACCESS_KEY_ID beat profile files, which is a common cause of using the wrong identity.
  • Prefer SSO profiles for people and roles for workloads; long-lived access keys are a last resort.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
aws configure sso                 # answer prompts: session name, start URL, SSO Region
aws sso login --profile sandbox-admin
aws sts get-caller-identity --profile sandbox-admin
# {
#   "UserId": "AROAEXAMPLEID:alice",
#   "Account": "111122223333",
#   "Arn": "arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_AdministratorAccess_0123abcd/alice"
# }
aws configure list --profile sandbox-admin   # where each value came from

The assumed-role ARN shows the permission set name inside the AWSReservedSSO role name.

A safety check at the top of every lab script

You are about to run a script that deletes lab resources. Step 1: the script asks STS who is calling. Step 2: it compares the account with the sandbox ID you expect. Step 3: it prints the Region it will use. If either check fails it stops before doing any damage. Expected evidence: the script prints the account, ARN and Region, then carries on only in the sandbox.

bash
#!/usr/bin/env bash
set -euo pipefail
EXPECTED=111122223333
ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
ARN=$(aws sts get-caller-identity --query Arn --output text)
REGION=$(aws configure get region || true)
echo "Account $ACCOUNT as $ARN in ${AWS_REGION:-$REGION}"
if [ "$ACCOUNT" != "$EXPECTED" ]; then
  echo "Not the sandbox account, stopping." >&2; exit 1
fi

Debugging the wrong identity

You ran aws s3 ls --profile sandbox-admin and got AccessDenied, which is odd for an admin. Step 1: run get-caller-identity with the same profile. The ARN shows an IAM user called old-deploy, not your SSO role. Step 2: list AWS variables in your shell and spot leftover AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from an old tutorial; environment credentials beat the profile. Step 3: unset them and try again. Step 4: get-caller-identity now shows the AWSReservedSSO role. Step 5: delete the old access key in IAM so it cannot be used again.

bash
aws sts get-caller-identity --profile sandbox-admin
env | grep '^AWS_'
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
aws sts get-caller-identity --profile sandbox-admin
aws configure list --profile sandbox-admin

Quick reference

  • aws configure sso: create an SSO profile; aws sso login --profile NAME: sign in
  • aws sts get-caller-identity: account, ARN and user ID; needs no permissions
  • aws configure list: shows each setting and where it came from
  • Region order (CLI v2): --region, AWS_REGION, AWS_DEFAULT_REGION, profile region
  • Env credentials beat profile files; unset stale AWS_* variables
  • Temporary access key IDs start with ASIA; long-lived ones start with AKIA
  • AWS_PROFILE picks a default profile; --profile overrides it

Common mistakes

  • Leaving old AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY variables set in your shell, so every command silently uses them.
  • Running a script without a Region set and getting an error, or creating resources in an unexpected Region.
  • Copying temporary credentials into files and forgetting they expire, then debugging an ExpiredToken error.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Your shell has AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY set, and you run a command with no --profile. Which credentials are used?

Question 2

2What does aws sts get-caller-identity require?

Question 3

3Which file does aws configure sso update?

Question 4

4You pass --region us-west-2 while AWS_REGION is eu-west-1. Where does the request go?

Question 5

5An ARN reads arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnly_9f8e/alice. What is it?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Configure an SSO profile called sandbox-admin and confirm your identity.

  2. Exercise 2medium

    Show how Region precedence works by running the same harmless command three ways and recording which Region answered.

  3. Exercise 3hard

    Write a short Python script with boto3 that prints the caller identity and the source of its credentials, and run it once with an SSO profile and once with environment credentials exported from that profile.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

How long do SSO credentials last?
Role credentials last for the permission set's session duration, which defaults to one hour and can be set up to 12 hours. The CLI refreshes them automatically while your SSO sign-in is still valid.
Should I still use aws configure with access keys?
Only when there is no better option. For people, use aws configure sso; for code on AWS, use roles; for CI, use OIDC federation.
Why does my command say ExpiredToken?
Your temporary credentials or SSO token have expired. Run aws sso login again for SSO profiles, or fetch fresh credentials if you exported them manually.
What does the SDK do on an EC2 instance with no configuration?
It reaches the end of the provider chain and asks the instance metadata service for the attached role's credentials, refreshing them before they expire.

Go deeper

ShareXLinkedInWhatsApp