Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash with AWS CLI - unable to locate credentials

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!

like image 873
Smajl Avatar asked Jul 15 '15 08:07

Smajl


People also ask

How do you fix unable to locate credentials you can configure credentials by running AWS configure?

While you might have your credentials and config file properly located in ~/. aws, it might not be getting picked up by your user account. To set the credentials, run this command: aws configure and then enter the credentials that are specified in your ~/. aws/credentials file.

Can't find AWS credentials file?

The shared AWS config and credentials files are plaintext files that reside by default in a folder named . aws that is placed in the " home " folder on your computer. On Linux and macOS, this is typically shown as ~/. aws .

How can I fix the error unable to locate credentials when I try to connect to my Amazon s3 bucket using the AWS CLI?

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.


2 Answers

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.

like image 195
meuh Avatar answered Sep 25 '22 18:09

meuh


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.

like image 31
Himanshu Gupta Avatar answered Sep 22 '22 18:09

Himanshu Gupta