The AWS CLI, SDKs and credential profiles

Beginner · 14 min

How the CLI and SDKs find a Region and credentials: profiles, config files, environment variables, the provider chain, aws login and SSO.

Before thisRegions, endpoints, ARNs and service scopeCreating an account and securing the root user

Rahul Wagh · 14 min

The AWS CLI, SDKs and credential profiles

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Rahul WaghReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The CLI and every SDK call the same HTTPS APIs. What differs is only how you phrase the request.
  • A profile is a named bundle of settings in ~/.aws/config and ~/.aws/credentials. Choose it with --profile or AWS_PROFILE.
  • Credentials come from a provider chain: sources are tried in order and the first that yields credentials wins.
  • Access keys in environment variables beat a profile chosen with AWS_PROFILE. An explicit --profile behaves differently.
  • Prefer temporary credentials: IAM Identity Center or aws login for people, roles for workloads. Avoid long-lived keys in files.
  • When something is odd, run aws sts get-caller-identity and aws configure list before reading any policy.

One API, many front ends

Everything in AWS is an HTTPS API. The console is a website that calls it. The CLI turns aws s3api list-buckets into a request. An SDK such as boto3 or the AWS SDK for JavaScript does the same from code. Learn the API’s operation names once and you can move between all three.

Every call needs two things your command rarely mentions: a Region (which endpoint) and credentials (who is asking). Most “it works for him but not for me” problems are one of those two coming from somewhere unexpected.

Use CLI version 2. Version 1 is in maintenance mode with end of support announced, and newer features such as aws login are version 2 only.

Profiles

Settings live in two plain files. ~/.aws/config holds behaviour, ~/.aws/credentials holds secrets. Each section is a profile:

# ~/.aws/config
[default]
region = eu-west-1

[profile dev]
region = eu-central-1

[profile prod-admin]
source_profile = dev
role_arn = arn:aws:iam::123456789012:role/admin
region = eu-west-1

Note the inconsistency that catches everyone: config writes [profile dev], while credentials writes just [dev].

prod-admin holds no keys. It says: use dev’s credentials to assume a role, and use what comes back. The tool makes that call and refreshes it for you.

The provider chain

Tools don’t expect credentials in one place. They try a list of sources in order and stop at the first hit. For the CLI it is roughly:

  1. Command line options such as --profile
  2. Environment variables
  3. Role assumption configured in the profile, including web identity
  4. IAM Identity Center sessions
  5. The credentials file, an external process, then the config file
  6. Container credentials (ECS)
  7. The EC2 instance profile

Each SDK’s chain differs a little, but the shape is the same, and it is why identical code runs on a laptop and on a server. Think of it as checking your pockets in a fixed order for a key: the first one you find is the one you try, even if it opens the wrong door.

Watching it resolve

aws configure list shows each value and where it came from. With a profile file in place and keys also exported in the shell:

$ AWS_PROFILE=dev aws configure list
      Name                    Value             Type    Location
   profile                      dev              env    ['AWS_DEFAULT_PROFILE', 'AWS_PROFILE']
access_key     ****************ting              env
secret_key     ****************ting              env
    region             eu-central-1      config-file    .../config

$ aws configure list --profile dev
      Name                    Value             Type    Location
   profile                      dev           manual    --profile
access_key     ****************MPLE  shared-credentials-file
secret_key     ****************cret  shared-credentials-file
    region             eu-central-1      config-file    .../config

The same profile, two different identities. With AWS_PROFILE, the leftover environment keys still win, while the Region comes from dev. That combination is how people run a command against the wrong account believing they switched. With --profile, the profile’s keys were used.

The prod-admin profile resolves differently again: the type column reads assume-role, and aws sts get-caller-identity reports an assumed-role/admin/... ARN rather than a user.

aws sts get-caller-identity

That command needs no permissions and answers “who am I right now?”. Run it first, every time.

Getting credentials without long-lived keys

aws configure asks for an access key ID and secret, and writes them to disk in plain text. They work until someone deactivates them, which is why leaked keys are such a common way in. There are better options.

IAM Identity Center is the usual answer in an organization. aws configure sso sets up an sso-session block once, and aws sso login opens a browser:

[sso-session work]
sso_start_url = https://example.awsapps.com/start
sso_region = eu-west-1

[profile dev]
sso_session = work
sso_account_id = 111122223333
sso_role_name = Developer
region = eu-west-1

aws login (CLI 2.32.0 or later) uses your existing console sign-in, as an IAM user, federated identity or even root, and keeps temporary credentials refreshed for up to 12 hours. Non-root identities need the SignInLocalDevelopmentAccess policy.

Workloads should use neither. Give them a role and let the chain find it.

Habits worth forming

  • Keep [default] harmless, or leave it out, so a forgotten --profile fails instead of touching production.
  • Put the Region in every profile.
  • In code, don’t pass keys to the client constructor. Let the chain work.
  • Use --query and --output to filter results, and --debug when you need to see the request and where credentials were found.

About the video

The embed is Rahul Wagh’s “AWS Config, Credentials file and profiles”, April 2024, 14 minutes. Its chapters cover the config files, how config differs from credentials, where the files live, creating credentials, adding profiles and switching between them. It uses access keys, which is fine for learning the file layout but not the best way to authenticate today, and it predates aws login. I judged it from chapters and description, since transcripts couldn’t be fetched. The re:Invent talk in deeper introduces aws login.

What was run: the configure list output and the role-assuming profile ran on AWS CLI 1.44.87 against moto, a local emulator, with made-up keys in throwaway files. aws login and Identity Center sign-in were not run and come from the documentation, as does the precedence list, which describes version 2.

Where this fits

The tool has credentials and an endpoint. Next is what actually travels over the wire: how API requests are signed, throttled and retried.

Check yourself

  1. 01You export AWS_PROFILE=dev, but commands still act on the production account. What is the likely cause?

    AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are still set in that shell, and environment keys come before profile files in the chain. Unset them, then check with aws configure list.

  2. 02Code works on your laptop and on an EC2 instance without any change and without keys in the code. How?

    The SDK walks its credential chain. On the laptop it finds your profile or login session, and on the instance it reaches the instance profile credentials from the metadata service.

  3. 03What is the difference between source_profile and role_arn in a profile?

    role_arn says which role to assume, and source_profile says whose credentials to use to call AssumeRole. The tool performs the call and refreshes the temporary credentials for you.

  4. 04Why is aws configure with an access key the last resort for a human user?

    It stores a long-lived secret in a plain text file that works until somebody revokes it. Identity Center and aws login give short-lived credentials that expire on their own.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark