Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Terraform cannot validate its AWS credentials (sts:GetCallerIdentity fails)

ErrorError: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity

Short answer

This error means the AWS provider found no working credentials, or found the wrong ones. It checks them with an STS call before anything else. Ask the AWS CLI who you are with the same environment. Then look for stale environment variables, the wrong profile, a missing session token, or a region that is not set.

  • Applies to Terraform 1.x, AWS provider 6.x, AWS CLI 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the call Terraform makes, in the same shell and with the same environment. If it fails here, the problem is your credentials, not your Terraform code.

    aws sts get-caller-identity
  2. 2Match what you see, then jump to its fix

    1. 1get-caller-identity returns an account or user you did not expect, or an expired-token error, although your ~/.aws files are correctOld environment variables override the profile you meant to useFix
    2. 2The identity call fails with no credentials, or a profile-not-found messageThe profile name is wrong or not selectedFix
    3. 3The call fails even though the access key and secret look right; the keys come from an assumed role or a single sign-on loginTemporary credentials are used without their session tokenFix
    4. 4No region appears in the environment, the profile or the provider blockNo region is set anywhereFix

What does the error look like?

  • Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity

Cause 1 of 4

Old environment variables override the profile you meant to use

1Check

The provider reads credentials in a fixed order: provider block arguments, then environment variables, then the shared credentials file. A leftover AWS_ACCESS_KEY_ID in the shell therefore beats your profile.

env | grep '^AWS_'

2Fix

Unset the variables you do not want, so the next source in the order is used.

unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN

3Verify

Ask AWS who you are again, then run a plan.

aws sts get-caller-identity
terraform plan

Confirmed by registry.terraform.io

Did this fix it?

Cause 2 of 4

The profile name is wrong or not selected

1Check

Without a profile, the provider uses [default]. If your keys live under another profile name, it has to be selected with the profile argument or the AWS_PROFILE variable.

aws configure list-profiles
echo "$AWS_PROFILE"

2Fix

Select the profile for this shell, or set it in the provider block.

export AWS_PROFILE=my-profile

# or in main.tf
provider "aws" {
  profile = "my-profile"
}

3Verify

The identity call now answers with the account you expect.

aws sts get-caller-identity --profile my-profile

Confirmed by registry.terraform.io

Did this fix it?

Cause 3 of 4

Temporary credentials are used without their session token

1Check

Temporary credentials from AWS STS come as three values. The access key and secret alone are not enough: the session token must travel with them, and the CLI's environment variable for it is AWS_SESSION_TOKEN.

env | grep -c '^AWS_SESSION_TOKEN='

2Fix

Export all three values together, or switch to a profile that fetches them for you.

export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_SESSION_TOKEN=...

3Verify

Run the identity call and a plan again.

aws sts get-caller-identity
terraform plan

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

No region is set anywhere

1Check

The region argument is optional in the provider block, but a region must still come from somewhere: the provider block, AWS_REGION or AWS_DEFAULT_REGION, the profile in the shared config file, or instance metadata.

echo "$AWS_REGION $AWS_DEFAULT_REGION"
aws configure get region

2Fix

Set it in the provider block so it no longer depends on the machine.

provider "aws" {
  region = "us-east-1"
}

3Verify

Initialise and plan again.

terraform init
terraform plan

Confirmed by registry.terraform.io

Did this fix it?

How do I stop it coming back?

  • Pick one credential source per project (a named profile or CI role) and say so in the provider block.
  • Do not export long-lived keys in shell startup files; they silently win over profiles.
  • In CI, fetch fresh temporary credentials for each run instead of reusing old ones.
  • Treat skip_credentials_validation as a test-only switch: it hides the problem, it does not fix it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

Apache has a certificate, but https:// does not open

No line with :443 in the output of ss -ltnp

HTTPS needs four things at once: mod_ssl loaded, Apache listening on 443, a virtual host on 443 with SSLEngine on, and a firewall that lets 443 in.

4 causes, each with a fix

Servers

AWS CLI says you must specify a region

You must specify a region. You can also configure your region by running "aws configure".

The AWS CLI sends every request to one region and has none to use.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.