Configure AWS credentials correctly in the AWS CLI to resolve the error “Unable to locate credentials”. As a part of our AWS Support Services, Bobcares provides answers to all Amazon S3 queries.
Error “Unable to locate credentials” while using AWS CLI
The AWS CLI stores sensitive credential information that we specify with AWS configured in a local file named credentials, in a folder named .aws in the home directory. An AWS CLI “Unable to locate credentials” error appears when Amazon S3 can’t find the credentials to authenticate AWS API calls. This usually occurs when:
- The AWS credentials are not properly configured in the AWS CLI.
- Utilizing an outdated AWS CLI version.
How To Fix The Error “Unable to locate credentials” while using AWS CLI?
In order to fix the error, our Support team suggests confirming the AWS credentials are correctly configured in the AWS CLI. We can use the following command to see if the AWS CLI is set up with credentials:
$ aws configure list
With credentials configured in the config file, the command returns a response similar to the following:
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
If credentials are set up using environment variables, the command responds with something like this:
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************N36N env
secret_key ****************cxxy env
region <not set> None None
The command provides a response that is like the following if the credentials are configured in an instance profile:
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************YVEQ iam-role
secret_key ****************2a9N iam-role
region <not set> None None
No credentials are configured If the command returns the following output:
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
To fix the AWS CLI unable to locate credentials error, we have to analyze the response to see whether credentials are missing or incorrect.
How To Set And View Credentials?
We can use the aws configure
to set and view the credentials, Region, and output format. For 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
The command aws configure set
can be used to set any credentials or configuration settings using aws configure set. Specify the profile that we want to view or modify with the –profile setting. For example, the below command sets the region in the profile named bobcares.
aws configure set region us-west-2 --profile bobcares
To remove a setting, use an empty string as the value, or manually delete the setting in the config and credentials files in a text editor.
aws configure set cli_pager "" --profile bobcares
We also have to make sure to use the most recent AWS CLI version to avoid error.
Let’s see a case when one of the users encounters this AWS CLI “unable to locate credentials” error and how our Support team assisted with the issue. While the user uses the shell script to download some files from S3 and mount an EBS drive, he ends up with the message, “Unable to locate credentials”. The code the customer used is as follows:
#!/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):
From reviewing the code, we understand the user runs the script with sudo. However, they specify the credentials as a normal user. Our Support team suggests the user do everything with AWS as root or as a normal user to fix unable to locate credentials in AWS CLI. This happens because sudo will change the $HOME directory to /root, and remove most bash variables like AWS_CONFIG_FILE from the environment.
[Looking for a solution to another query? We are just a click away.]
Conclusion
An “Unable to locate credentials” error indicates that Amazon S3 can’t find the credentials to authenticate AWS API calls. In this article, our Support team explains the method to fix the error efficiently.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
var google_conversion_label = "owonCMyG5nEQ0aD71QM";
#!/bin/bash
# Build and push ComfyUI image to ECR
if [ -z “$1” ]
then
echo “Usage: build_and_push.sh ”
exit 1
fi
REGION=$1
ACCOUNT_ID=$(aws sts get-caller-identity –query Account –output text)
echo “AccountID: $ACCOUNT_ID, Region: $REGION”
docker build –platform=”linux/amd64″ . -t comfyui-images
docker tag comfyui-images:latest ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/comfyui-images:latest
aws ecr get-login-password –region ${REGION} | docker login –username AWS –password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
docker push ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/comfyui-images:latest
docker images|grep none|awk ‘{print $3}’|xargs -I {} docker rmi -f {}
Hello,
Please contact our support team via live chat(click on the icon at right-bottom).