Skip to content
ohsudo, Home
Cloud

AWS CLI "Unable to locate credentials": where the CLI looks, and why it found nothing

Fix AWS CLI "Unable to locate credentials": check the profile and user, environment variables, SSO and aws login, EC2 instance roles, IMDS in containers, and cron jobs.

Beginner4 min readApplies to AWS CLI v2 · AWS SDKs · Amazon EC2 · ECS · EKS · Docker · Linux · macOS · Windows

by OhSudo ··

Checked on real software: AWS CLI 2 · November 2026How we check

$ aws s3 ls
aws: [ERROR]: An error occurred (NoCredentials): Unable to locate credentials. You can configure credentials by running "aws login".

Older versions of AWS CLI v2 end with by running "aws configure", without the [ERROR] prefix.

The AWS CLI looked for credentials in every place it knows, in order, and found none. It hasn’t contacted AWS yet, so this isn’t about permissions. The question is why none of those places had anything for the user, profile and machine where the command ran.

  1. Command-line options--profile
  2. Environment variablesAWS_ACCESS_KEY_ID…, AWS_PROFILE
  3. ~/.aws/credentials and configincluding SSO and role profiles
  4. Container credentialsECS tasks, EKS Pod Identity
  5. EC2 instance rolefrom the metadata service(breaks here)

↻ Nothing found in any of them: Unable to locate credentials.

The CLI looks in these places in order, and uses the first one that has credentials. Here none did, so it never contacted AWS.

TL;DR

Terminal window
aws configure list # what the CLI found, and where
aws sts get-caller-identity # who you are, once it works
aws login # sign in through the browser (recent CLI versions)
aws sso login --profile prod # or log in with IAM Identity Center
aws configure # or set access keys for this user

1. See where it looked

Terminal window
aws configure list
NAME : VALUE : TYPE : LOCATION
profile : <not set> : None : None
access_key : <not set> : None : None
secret_key : <not set> : None : None
region : eu-west-1 : config-file : ~/.aws/config

The CLI checks, in this order:

  1. command-line options such as --profile;
  2. environment variables: AWS_ACCESS_KEY_ID with AWS_SECRET_ACCESS_KEY (and AWS_SESSION_TOKEN), or AWS_PROFILE;
  3. ~/.aws/credentials and ~/.aws/config, including SSO and role profiles;
  4. a container’s credentials (ECS tasks, EKS Pod Identity);
  5. the EC2 instance’s role, from the instance metadata service (IMDS).

2. Another user, another home folder

Credentials in ~/.aws/ belong to one user:

  • sudo aws … runs as root and reads /root/.aws/, not yours. Drop sudo, or use sudo -E carefully.
  • cron and systemd run with a minimal environment, often as another user, so environment variables from your shell aren’t there. Use a profile in that user’s ~/.aws/, or the instance role.
  • Docker: a container has none of the host’s files. Mount them read-only, -v ~/.aws:/root/.aws:ro, or pass the variables with -e. On AWS, prefer the task or instance role.

3. A named profile

With profiles, the CLI uses default unless told otherwise. If you only have [prod], plain aws s3 ls finds nothing. Choose one:

Terminal window
aws s3 ls --profile prod
export AWS_PROFILE=prod

A profile name that doesn’t exist gives another message: The config profile (prod) could not be found.

aws login, in recent versions, signs you in through the browser with the same credentials as the AWS console, and keeps short-lived credentials for the CLI.

For IAM Identity Center (SSO) profiles, run aws sso login --profile prod when the session expires. An expired SSO session usually says so with its own message about the token.

4. On EC2: the instance role

An instance without a role attached has no credentials to give. Check in the EC2 console (Actions → Security → Modify IAM role), or from the instance:

Terminal window
TOKEN=$(curl -s -X PUT http://169.254.169.254/latest/api/token -H 'X-aws-ec2-metadata-token-ttl-seconds: 60')
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/iam/security-credentials/

That prints the role’s name, or nothing when there’s none.

In a container on EC2, the instance has a role and the container still finds nothing: with IMDSv2, the instance answers only as many network hops as HttpPutResponseHopLimit allows. It’s 1 on many instances (newer AMIs, such as Amazon Linux 2023, set 2), and the container’s traffic takes two. Check it with aws ec2 describe-instances --instance-ids i-0abc123 --query 'Reservations[].Instances[].MetadataOptions', and raise it to 2:

Terminal window
aws ec2 modify-instance-metadata-options --instance-id i-0abc123 --http-put-response-hop-limit 2 --http-endpoint enabled

On ECS, use a task role. On EKS, use EKS Pod Identity or IAM roles for service accounts, rather than the node’s role.

5. In CI

Use OpenID Connect so the pipeline gets short-lived credentials without stored keys. In GitHub Actions, aws-actions/configure-aws-credentials with role-to-assume, and permissions: id-token: write in the workflow. A step that runs before it, or in another job, has no credentials.

Warning

Don’t paste access keys into scripts, Dockerfiles, compose.yaml, or a repository. Keys in an image or in Git history leak, and bots look for them on GitHub within minutes. Use roles where AWS offers them, and short-lived credentials everywhere else.

Shell, we have a problem!

Similar, but different: is not authorized to perform means AWS knows who you are, and denies the action. AccessDenied from S3 is the same, for a bucket. InvalidClientTokenId or SignatureDoesNotMatch means credentials were found, and they’re wrong or deleted. ExpiredToken means temporary credentials ran out. On Azure, the same thing is Please run 'az login'. For everyday commands, see the AWS CLI cheat sheet. Credentials found, but too old: ExpiredToken.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Articles

Related articles