Skip to content

Instantly share code, notes, and snippets.

@wyllie
Created July 22, 2018 17:04
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wyllie/c99a0ccba64af5e3e6ca901c7a2c9e5d to your computer and use it in GitHub Desktop.
Save wyllie/c99a0ccba64af5e3e6ca901c7a2c9e5d to your computer and use it in GitHub Desktop.
Parse aws credentials file in bash
#!/usr/bin/env bash
INI_FILE=~/.aws/credentials
while IFS=' = ' read key value
do
if [[ $key == \[*] ]]; then
section=$key
elif [[ $value ]] && [[ $section == '[default]' ]]; then
if [[ $key == 'aws_access_key_id' ]]; then
AWS_ACCESS_KEY_ID=$value
elif [[ $key == 'aws_secret_access_key' ]]; then
AWS_SECRET_ACCESS_KEY=$value
fi
fi
done < $INI_FILE
echo $AWS_ACCESS_KEY_ID
echo $AWS_SECRET_ACCESS_KEY
@todgru
Copy link

todgru commented Feb 9, 2021

If available and depending on use case, the aws cli tool can retrieve the values, aws configure get varname [--profile profile-name]

aws --profile default configure get aws_access_key_id
aws --profile default configure get aws_secret_access_key

@mailsanchu
Copy link

@todgru you solution is better but it is reading the file 2 times

@peterhodes
Copy link

Great work Wylie - as a helpful addition can I suggest you use the $AWS_CONFIG_FILE variable instead of hardcoding the "~/.aws/credentials" file.

@adiii717
Copy link

@todgru amazing, I never tried that before

@Arushacked
Copy link

If you just need to use aws credentials in a bash script to call aws CLI api, then you use just this one liner at the top of script.
export AWS_PROFILE=user1

@halamirsg
Copy link

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment