To fix the “Unable to locate credentials” error, configure AWS credentials correctly in the AWS CLI. All Amazon S3 questions are answered by Skynats as a part of our AWS Support Services.
Error “Unable to locate credentials” while using AWS CLI
In a local file called credentials, located in a folder called.aws in the home directory, the AWS CLI stores sensitive credential information that we specify with AWS configured. When Amazon S3 cannot locate the credentials to authenticate AWS API calls, an “Unable to locate credentials” error in the AWS CLI is displayed. This typically happens when:
- The AWS CLI’s configuration of the AWS credentials is incorrect.
- Using an outdated AWS CLI version
Fixing Error “Unable to locate credentials” while using AWS CLI
Our Support team advises verifying the AWS credentials are correctly configured in the AWS CLI to resolve the issue. We can check this by running the following command to see if the AWS CLI is set up with credentials:
$ aws configure list
The command returns a result that resembles the following when credentials are set in the configuration file:
Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************ABCD config_file ~/.aws/config secret_key ****************ABCD config_file ~/.aws/config region us-west-1 env AWS_DEFAULT_REGION
The command returns a result that looks like this if credentials were configured using environment variables:
Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************N36N env secret_key ****************cxxy env region <not set> None None
If the credentials are set up in an instance profile, the command returns a response that looks like this:
Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************YVEQ iam-role secret_key ****************2a9N iam-role region <not set> None None
If the command returns the following result, no credentials are configured.
Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key <not set> None None secret_key <not set> None None region <not set> None None
We must examine the response to determine whether credentials are missing or incorrect if we want to resolve the AWS CLI unable to locate credentials error.
How To Set And View Credentials?
The credentials, region, and output format can all be set and viewed using aws configure. E.g.,
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY Default region name [None]: us-west-2 Default output format [None]: json
You can set any credentials or configuration settings by using the command aws configure set. Use the -profile setting to specify the profile that you want to view or edit. For instance, the command below sets the region for the Skynats profile.
aws configure set region us-west-2 --profile skynats
Use an empty string as the setting’s value or manually remove the setting in the config and credentials files using a text editor to remove a setting.
aws configure set cli_pager "" --profile skynats
To prevent errors, we must also make sure to use the most recent AWS CLI version.
Here is an example of an AWS CLI “unable to locate credentials” error and how our Support team helped the user resolve it. The user receives the error message “Unable to locate credentials” after attempting to mount an EBS drive and download some files from S3 using the shell script. The customer entered the following code:
#!/bin/bash AWS_CONFIG_FILE="~/.aws/config" echo $1 sudo mkfs -t ext4 $1 sudo mkdir /s3-backup-test sudo chmod -R ugo+rw /s3-backup-test sudo mount $1 /s3-backup-test sudo aws s3 sync s3://backup-test-s3 /s3-backup/test du -h /s3-backup-test ipt (short version):
We can infer from examining the code that the user runs the script with sudo. They do, however, specify the credentials for a standard user. To resolve the issue of being unable to locate credentials in AWS CLI, our Support team recommends that the user perform all AWS operations as root or as a normal user. This occurs because sudo changes the $HOME directory to /root and removes most bash variables such as AWS_CONFIG_FILE from the environment.
Are you looking for an answer to another query? Contact our technical support team.