Skip to content

Instantly share code, notes, and snippets.

@NimaSaed
Last active February 18, 2022 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NimaSaed/8d8989e6e662359ba7c3a20e42e2a419 to your computer and use it in GitHub Desktop.
Save NimaSaed/8d8989e6e662359ba7c3a20e42e2a419 to your computer and use it in GitHub Desktop.
Bash AWS Profile Changer
# Add this function to the end of your ~/.bashrc
function aws_profile() {
local aws_home="$HOME/.aws"
local profiles=(`\
cat ${aws_home}/config | \
grep profile | \
sed 's/\[//g;s/\]//g' | \
cut -d " " -f 2`);
PS3="Select a profile: [none = 0] "
select profile in ${profiles[@]}
do
selected=$profile;
break;
done
unset $PS3;
if [ ! -z ${profile} ];
then
export AWS_PROFILE="${profile}";
export AWS_REGION=$(cat ${aws_home}/config | sed -n "/${profile}/,/\[/p" | grep region | cut -d '=' -f 2 | sed 's/ //g')
export AWS_ACCESS_KEY_ID=$(cat ${aws_home}/credentials | sed -n "/${profile}/,/\[/p" | grep aws_access_key_id | cut -d '=' -f 2 | sed 's/ //g')
export AWS_SECRET_ACCESS_KEY=$(cat ${aws_home}/credentials | sed -n "/${profile}/,/\[/p" | grep aws_secret_access_key | cut -d '=' -f 2 | sed 's/ //g')
else
AWS_PROFILE=""
AWS_REGION=""
AWS_ACCESS_KEY_ID=""
AWS_SECRET_ACCESS_KEY=""
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment