Created
December 23, 2019 18:23
-
-
Save nh13/6af5682cb49ac06fe7f15b65c7359735 to your computer and use it in GitHub Desktop.
Bash function for switching AWS profiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function aws_switch { | |
last_profile=$(cat ~/.aws/.last_profile | xargs) | |
last_profile_i=1; | |
i=1 | |
profiles=() | |
for profile in $(grep "^\[.*\]$" ~/.aws/credentials | sed -e 's_^\[__' -e 's_\]$__' | sort) | |
do | |
echo "[$i] $profile"; | |
profiles+=( "$profile" ); | |
if [ $last_profile == $profile ]; then | |
last_profile_i=$i; | |
fi | |
let i=i+1; | |
done | |
while true; do | |
read -p "Pick the AWS profile you wish to use [default: $last_profile]:" profile | |
profile=${profile:-$last_profile_i} | |
if [[ 1 -le $profile ]] && [[ $profile -le $i ]]; then | |
let profile_i=$profile-1; | |
export AWS_DEFAULT_PROFILE=${profiles[$profile_i]}; | |
echo "Switched to AWS profile [${profiles[$profile_i]}]"; | |
echo "${profiles[$profile_i]}" > ~/.aws/.last_profile | |
break; | |
else | |
echo "Invalid profile #: $profile" | |
break; | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment