Last active
November 18, 2024 20:16
-
-
Save soren/dbfd37fc2ee66dec1f686012dfa23855 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# INTRODUCTION | |
# | |
# Defines a function called aws-profile to list and select AWS profile | |
# to use. The function is meant for interactive use. You use it to | |
# select and set the current AWS Profile by setting the AWS_P#ROFILE | |
# environment variable. | |
# | |
# Note: You need the AWS CLI installed and configured. This should | |
# create the $HOME/.aws/credentials file that the function reads. | |
# | |
# | |
# INSTALLATION | |
# | |
# This script is available in the following Gist: | |
# https://gist.github.com/soren/dbfd37fc2ee66dec1f686012dfa23855 | |
# | |
# You can either copy-n-paste from the Gist or clone the Gist using | |
# git, like this: | |
# | |
# cd <some directory> | |
# git clone https://gist.github.com/dbfd37fc2ee66dec1f686012dfa23855.git aws-profile | |
# | |
# Then add the following line to your ~/.bashrc: | |
# | |
# source "<some directory>/aws-profile/aws-profile.sh" | |
# | |
# | |
# USAGE | |
# | |
# When called, the function will display a numbered list of the names | |
# of all your AWS profiles. | |
# | |
# You will then be prompted to select a profile by entering the | |
# corresponding number from the list. | |
# | |
# If you don't enter a number, nothing will happen. | |
# | |
# If you enter a number that does not correspond to a number, you will | |
# be prompted again. | |
# | |
# If you select a profile that is currently set, that is the | |
# AWS_PROFILE environment variable is set to this, then a message | |
# notifying you will be printed and nothing will happen. | |
# | |
# Finally, if you select a profile that is not currently set, then the | |
# AWS_PROFILE environment variable will be set and the AWS Account ID | |
# for the profile will be retrieved. If the Account ID cannot be | |
# retrieved you will be notified, but the profile will still be | |
# selected, else a message telling you that you switched to the | |
# profile will be displayed. | |
# | |
function aws-profile() { | |
local -a profiles | |
local i a profile cmd account | |
local aws_creds_file="$HOME/.aws/credentials" | |
if [[ ! -f "$aws_creds_file" ]]; then | |
echo "AWS credentials file (${aws_creds_file}) not found!" | |
return | |
fi | |
mapfile -t profiles < <(grep -oP '(?<=\[).+(?=\])' "$aws_creds_file" | sort) | |
for ((i=0; i<${#profiles[@]}; i++)); do | |
printf "%02d - %s\n" $((i+1)) "${profiles[$i]}" | |
done | column | |
echo | |
while true; do | |
echo -n "Select profile: " | |
read -r a | |
[[ -z $a ]] || [[ $a -gt 0 && $a -le ${#profiles[@]} ]] && break; | |
done | |
[[ -z $a ]] && return | |
profile=${profiles[$((a-1))]} | |
if [[ $AWS_PROFILE == "$profile" ]]; then | |
echo "The ${profile} profile is already selected." | |
return | |
fi | |
cmd="AWS_PROFILE=${profile}" | |
eval "$cmd" | |
echo | |
account=$(aws sts get-caller-identity --query "Account" --output text 2>&1) \ | |
&& echo "Switched to AWS Profile=${profile}, Account ID=${account}" \ | |
|| echo "Your credentials for ${profile} are invalid!" | |
echo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now part of my AWS CLI Utilities for Bash, i.e., I won't be updating this gist.