profile=$1 [ $# -gt 0 ] && shift region=${1:-""} declare -A symRegions symRegions[ire1]='eu-west-1' symRegions[fra1]='eu-central-1' symRegions[ohio1]='us-east-2' lookup_region=${symRegions[$region]} [ -z $lookup_region ] || region=${lookup_region} if [ ! -f ~/.aws/config ]; then echo "no aws config file found, bailing out..." return 1 fi if [ $profile ]; then # We got a profile pattern, look for a match # look for an exact match grep -qE "\[profile $profile\]" ~/.aws/config if [ $? -eq 0 ]; then # We have an exact match match=$profile else # Look for regex match match=$(grep -E "\[profile .*$profile.*" ~/.aws/config | sed -E 's/\[profile (.+)\]/\1/') # Array of matching profiles match_no=$(echo $match|wc -l) # Number of profiles matching if [ $match_no -gt 1 ]; then # more than one match print -P "$FX[bold]multiple profile match:" print -P "$FG[003]" echo $match print -P "$FX[reset]" return fi fi if [ $match ]; then # Single match, setting profile # export AWS_CLI_AUTO_PROMPT=off # Set default profile and profile export AWS_DEFAULT_PROFILE=$match export AWS_PROFILE=${AWS_DEFAULT_PROFILE} # Get default region and set region to argument or default region export AWS_DEFAULT_REGION=$(aws configure get region) export AWS_REGION=${region:-$AWS_DEFAULT_REGION} print -P "$FX[bold]activating profile $FG[075]$match$FX[reset]$FX[bold] on region $FG[075]$AWS_REGION$FX[reset]" # export AWS_CLI_AUTO_PROMPT=on return else print -P "$FX[bold]$FG[009]no match for $profile$FX[reset]" return fi elif [ $AWS_PROFILE ]; then # no profile passed, clean up current one, logout unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_DEFAULT_REGION AWS_REGION print -P "$FX[bold]profile cleared$FX[reset]." return fi print -P "$FX[bold]available profiles" print -P "$FG[075]" grep profile ~/.aws/config | sed -E 's/\[profile (.+)\]/\1/' print -P "$FX[reset]" # vim: set ts=2 sw=2 tw=0 ft=sh :