if you want to remove an article from website contact us from top.

    unable to locate credentials. you can configure credentials by running aws configure.

    Mohammed

    Guys, does anyone know the answer?

    get unable to locate credentials. you can configure credentials by running aws configure. from screen.

    linux

    I have a shell script which is supposed to download some files from S3 and mount an ebs drive. However, I always end up with "Unable to locate credentials". I have specified my credentials with th...

    Bash with AWS CLI - unable to locate credentials

    Ask Question

    Asked 7 years, 4 months ago

    Modified 7 months ago

    Viewed 120k times 45

    I have a shell script which is supposed to download some files from S3 and mount an ebs drive. However, I always end up with "Unable to locate credentials".

    I have specified my credentials with the aws configure command and the commands work outside the shell script. Could somebody, please, tell me (preferably in detail) how to make it work?

    This is my script #!/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):

    Thanks for any help!

    linuxbashamazon-web-servicesaws-cli

    Share

    Improve this question

    edited Jul 23, 2021 at 8:13

    RtmY 15k10 10 gold badges 101 101 silver badges 106 106 bronze badges

    asked Jul 15, 2015 at 8:58

    Smajl 7,30728 28 gold badges 106 106 silver badges 175 175 bronze badges Add a comment

    11 Answers

    59

    sudo will change the $HOME directory (and therefore ~) to /root, and remove most bash variables like AWS_CONFIG_FILE from the environment. Make sure you do everything with aws as root or as your user, dont mix.

    Make sure you did sudo aws configure for example. And try

    sudo bash -c 'AWS_CONFIG_FILE=/root/.aws/config aws s3 sync s3://backup-test-s3 /s3-backup/test'

    You might prefer to remove all the sudo from inside the script, and just sudo the script itself.

    Share Improve this answer

    answered Jul 15, 2015 at 9:21

    meuh 10.7k2 2 gold badges 27 27 silver badges 44 44 bronze badges

    Thanks, you are absolutely right... this was the root of the problem (running the script with sudo but specifying the credentials as a normal user). Thanks –

    Smajl

    Jul 15, 2015 at 9:41

    Had this problem in a VS Code Remote Container: aws configure ran as root but a npm script executing aws codeartifact login couldn't access the ~/.aws/credentials file. Specifying the containerUser helped. –

    alexanderdavide

    May 3, 2021 at 19:37

    Add a comment 21

    While you might have your credentials and config file properly located in ~/.aws, it might not be getting picked up by your user account.

    Run this command to see if your credentials have been set:aws configure list

    To set the credentials, run this command: aws configure and then enter the credentials that are specified in your ~/.aws/credentials file.

    Share Improve this answer

    answered Jul 5, 2018 at 17:13

    Himanshu Gupta 6172 2 gold badges 8 8 silver badges 16 16 bronze badges Add a comment 17

    The unable to locate credentials error usually occurs when working with different aws profiles and the current terminal can't identify the credentials for the current profile.

    Notice that you don't need to fill all the credentials via aws configure each time - you just need to reference to the relevant profile that was configured once.

    From the Named profiles section in AWS docs:

    The AWS CLI supports using any of multiple named profiles that are stored in the config and credentials files. You can configure additional profiles by using aws configure with the --profile option, or by adding entries to the config and credentials files.

    The following example shows a credentials file with two profiles. The first [default] is used when you run a CLI command with no profile. The second is used when you run a CLI command with the

    --profile user1 parameter.

    ~/.aws/credentials (Linux & Mac) or %USERPROFILE%\.aws\credentials (Windows):

    [default]

    aws_access_key_id=AKIAIOSFODNN7EXAMPLE

    aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

    [user1]

    aws_access_key_id=AKIAI44QH8DHBEXAMPLE

    aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY

    So, after setting up the specific named profile (user1 in the example above) via aws configure or directly in the ~/.aws/credentials file you can select the specific profile:

    aws ec2 describe-instances --profile user1

    Or export it to terminal:

    $ export AWS_PROFILE=user1

    Share Improve this answer

    answered Jan 23, 2021 at 10:42

    RtmY 15k10 10 gold badges 101 101 silver badges 106 106 bronze badges 1

    I completely agree with the export way. The reason behind it because in some scripts you can't pass the AWS profile every time. So, it's better to use export AWS_PROFILE. –

    Mohit Sharma

    May 16, 2021 at 13:01

    Add a comment 13

    Answering in case someone stumbles across this based on the question's title.

    I had the same problem where by the AWS CLI was reporting unable to locate credentials.

    I had removed the [default] set of credentials from my credentials file as I wasn't using them and didn't think they were needed. It seems that they are.

    I then reformed my file as follows and it worked...

    [default]

    स्रोत : stackoverflow.com

    Resolve "Unable to locate credentials" error in Amazon S3

    How can I fix the error "Unable to locate credentials" when I try to connect to my Amazon S3 bucket using the AWS CLI?

    When I try to access my Amazon Simple Storage Service (Amazon S3) bucket using the AWS Command Line Interface (AWS CLI), I get an error. How do I fix this?

    Resolution

    An "Unable to locate credentials" error indicates that Amazon S3 can't find the credentials to authenticate AWS API calls. To resolve this issue, make sure that your AWS credentials are correctly configured in the AWS CLI.

    Note: If you still receive an error when running an AWS CLI command, make sure that you’re using the most recent AWS CLI version.

    To check if the AWS CLI is configured with credentials, run this command:

    $ aws configure list

    If your credentials are configured in the config file, the command returns a response similar to the following:

    Name Value Type Location

    ---- ----- ---- --------

    profile None None

    access_key ****************ABCD config_file ~/.aws/config

    secret_key ****************ABCD config_file ~/.aws/config

    region us-west-2 env AWS_DEFAULT_REGION

    If your credentials are configured using environment variables, then the command returns a response similar to the following:

    Name Value Type Location

    ---- ----- ---- --------

    profile None None

    access_key ****************N36N env

    secret_key ****************cxxy env

    region None None

    If your credentials are configured in an instance profile, the command returns a response similar to the following:

    Name Value Type Location

    ---- ----- ---- --------

    profile None None

    access_key ****************YVEQ iam-role

    secret_key ****************2a9N iam-role

    region None None

    If the command returns the following output, then no credentials are set:

    Name Value Type Location

    ---- ----- ---- --------

    profile None Noneaccess_key None Nonesecret_key None Noneregion None None

    Review the response to check whether credentials are missing or the stored credentials are incorrect. To update your credentials, use the AWS CLI, environment variables, or attach an instance profile to an EC2 instance.

    After you update your credentials, test the AWS CLI by running an Amazon S3 AWS CLI command, such as aws s3 ls.

    Related information

    Configuration and credential file settings

    Did this article help?

    Submit feedback

    Do you need billing or technical support?

    Contact AWS Support

    स्रोत : aws.amazon.com

    AWS CLI Unable To Locate Credentials

    Configure AWS credentials correctly in the AWS CLI to resolve the error "Unable to locate credentials". More...

    AWS CLI Unable To Locate Credentials | A Quick Fix

    by Shahalamol R | Sep 29, 2022

    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 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 None None

    access_key ****************N36N env

    secret_key ****************cxxy env

    region 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 None None

    access_key ****************YVEQ iam-role

    secret_key ****************2a9N iam-role

    region None None

    No credentials are configured If the command returns the following output:

    Name Value Type Location

    ---- ----- ---- --------

    profile None Noneaccess_key None Nonesecret_key None Noneregion 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.

    स्रोत : bobcares.com

    Do you want to see answer or more ?
    Mohammed 7 day ago
    4

    Guys, does anyone know the answer?

    Click For Answer