Skip to content

Instantly share code, notes, and snippets.

@tmclnk
Last active October 21, 2025 19:23
Show Gist options
  • Save tmclnk/d839d507ee9a3fc9734063c2dd796c2e to your computer and use it in GitHub Desktop.
Save tmclnk/d839d507ee9a3fc9734063c2dd796c2e to your computer and use it in GitHub Desktop.
AWS Profile Switcher for

AWS Profile Switcher

Creates aliases for aws cli profile management. Reads through your ~/.aws/config and presents a list of profiles.

  • aws-switch - switch profiles (with tab completion!)
  • aws-list - list all profiles in ~/.aws/config
  • aws-profile - show the current profile (whatever's in AWS_PROFILE)
  • aws-clear - clear out AWS_PROFILE env var

Make sure you're grabbing the file with the right extension for your shell!

ZSH

For vanilla ZSH, we'll store the function in its own file and source it from .zshrc

mkdir -p ~/.zsh/functions

Add to ~/.zshrc

source ~/.zsh/functions/aws-profile.zsh

Oh My ZSH

If using oh-my-zsh, I think you can just place the file in

~/.oh-my-zsh/custom/aws-profile.zsh

Bash

Place contents into ~/.bash_profile (or maybe ~/.bashrc, depending).

# AWS Profile Switcher for Bash
# Automatically creates aliases for all AWS profiles in ~/.aws/config
# Totally untested
# Function to switch AWS profile
aws-switch() {
if [ -z "$1" ]; then
echo "Current AWS Profile: ${AWS_PROFILE:-default}"
echo -e "\nAvailable profiles:"
grep '\[profile ' ~/.aws/config | sed 's/\[profile \(.*\)\]/ \1/'
return
fi
export AWS_PROFILE="$1"
echo "Switched to AWS profile: $AWS_PROFILE"
}
# Auto-generate aliases from AWS config
if [ -f ~/.aws/config ]; then
while IFS= read -r line; do
if [[ $line =~ \[profile\ (.+)\] ]]; then
profile_name="${BASH_REMATCH[1]}" # Changed from match to BASH_REMATCH
# Create a shorter alias by extracting key parts
if [[ $profile_name =~ AWS([A-Za-z]+)Access-([0-9]+) ]]; then
role=$(echo "${BASH_REMATCH[1]}" | tr '[:upper:]' '[:lower:]')
account="${BASH_REMATCH[2]}"
alias_name="aws-${role:0:5}-${account: -4}"
alias "$alias_name"="export AWS_PROFILE='$profile_name' && echo 'Switched to $profile_name'"
fi
# Also create alias with full profile name
alias "aws-${profile_name}"="export AWS_PROFILE='$profile_name' && echo 'Switched to $profile_name'"
fi
done < ~/.aws/config
fi
# Useful additional aliases
alias aws-profile='echo $AWS_PROFILE'
alias aws-clear='unset AWS_PROFILE && echo "Cleared AWS profile"'
alias aws-list='grep "\[profile " ~/.aws/config | sed "s/\[profile \(.*\)\]/ \1/"'
# Tab completion for aws-switch (bash version)
if command -v aws &> /dev/null; then
_aws_switch_complete() {
local cur profiles
cur="${COMP_WORDS[COMP_CWORD]}"
profiles=$(grep '\[profile ' ~/.aws/config 2>/dev/null | sed 's/\[profile \(.*\)\]/\1/')
COMPREPLY=( $(compgen -W "$profiles" -- "$cur") )
}
complete -F _aws_switch_complete aws-switch
fi
# AWS Profile Switcher for ZSH
# Automatically creates aliases for all AWS profiles in ~/.aws/config
# Function to switch AWS profile
aws-switch() {
if [ -z "$1" ]; then
echo "Current AWS Profile: ${AWS_PROFILE:-default}"
echo "\nAvailable profiles:"
grep '\[profile ' ~/.aws/config | sed 's/\[profile \(.*\)\]/ \1/'
return
fi
export AWS_PROFILE="$1"
echo "Switched to AWS profile: $AWS_PROFILE"
}
# Auto-generate aliases from AWS config
if [ -f ~/.aws/config ]; then
while IFS= read -r line; do
if [[ $line =~ \[profile\ (.+)\] ]]; then
profile_name="${match[1]}"
# Create a shorter alias by extracting key parts
# Format: role-account (e.g., power-650894713634)
if [[ $profile_name =~ AWS([A-Za-z]+)Access-([0-9]+) ]]; then
role=$(echo "${match[1]}" | tr '[:upper:]' '[:lower:]')
account="${match[2]}"
alias_name="aws-${role:0:5}-${account: -4}"
alias "$alias_name"="export AWS_PROFILE='$profile_name' && echo 'Switched to $profile_name'"
fi
# Also create alias with full profile name
alias "aws-${profile_name}"="export AWS_PROFILE='$profile_name' && echo 'Switched to $profile_name'"
fi
done < ~/.aws/config
fi
# Useful additional aliases
alias aws-profile='echo $AWS_PROFILE'
alias aws-clear='unset AWS_PROFILE && echo "Cleared AWS profile"'
alias aws-list='grep "\[profile " ~/.aws/config | sed "s/\[profile \(.*\)\]/ \1/"'
# Tab completion for aws-switch
if command -v aws &> /dev/null; then
_aws_profiles() {
local profiles
profiles=($(grep '\[profile ' ~/.aws/config 2>/dev/null | sed 's/\[profile \(.*\)\]/\1/'))
_describe 'aws profiles' profiles
}
compdef _aws_profiles aws-switch
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment