awsctx is your new best friend for managing AWS profiles. Think of it as the kubectx of AWS, but without the Kubernetes-induced existential crisis. It’s a lightweight Bash/Zsh function that helps you list, switch, and manage AWS profiles like a pro. No more fumbling around with export AWS_PROFILE—let awsctx do the heavy lifting for you.
- List Profiles: Quickly see all your AWS profiles with a single command.
- Highlight Current Profile: Because knowing where you are is half the battle.
- Switch Profiles: Change AWS profiles with ease. No eval, no drama.
- Color-Coded Output: Because plain text is for amateurs.
- Zero Dependencies: Just Bash or Zsh and the AWS CLI. If you don’t have those, we need to have a serious talk.
Copy the awsctx function below and paste it into your shell configuration file (~/.bashrc or ~/.zshrc):
# Simple AWS Profile Switcher
function awsctx() {
# Colors
local BLUE=$'\033[0;34m'
local YELLOW=$'\033[1;33m'
local NC=$'\033[0m'
# If no arguments, list profiles
if [ -z "$1" ]; then
echo "${BLUE}Available profiles:${NC}"
aws configure list-profiles | nl -w2 -s') '
echo "${BLUE}Current profile:${NC} ${YELLOW}${AWS_PROFILE:-default}${NC}"
return
fi
# Switch to specified profile
if aws configure list-profiles | grep -q "^${1}$"; then
export AWS_PROFILE="$1"
echo "${BLUE}Switched to profile:${NC} ${YELLOW}${AWS_PROFILE}${NC}"
else
echo "${YELLOW}Profile not found: $1${NC}"
return 1
fi
}After adding the function, reload your shell configuration to make it available in your current session:
source ~/.bashrc # For Bash users
source ~/.zshrc # For Zsh usersRun the following command to ensure awsctx is working:
awsctxIf you see a list of your AWS profiles and the current profile highlighted, congratulations—you’re ready to roll.
To list all available AWS profiles and highlight the current one, simply run:
awsctxExample output:
Available profiles:
1) default
2) dev
3) prod
Current profile: dev
To switch to a specific profile, pass the profile name as an argument:
awsctx <profile_name>Example:
awsctx prodOutput:
Switched to profile: prod
You can verify the current profile by running:
echo $AWS_PROFILEOutput:
prod
- AWS CLI Required: This function uses the
aws configure list-profilescommand, so make sure the AWS CLI is installed and configured on your system. - Default Profile: If no
AWS_PROFILEis set, thedefaultprofile is used. - Shell Compatibility: Works with Bash and Zsh. If you’re using Fish, well, you’re swimming upstream.
If you see this error, it means the profile you’re trying to switch to doesn’t exist. Double-check the profile name by running:
awsctxIf the profile is missing, make sure it’s defined in your AWS configuration file (~/.aws/config). You can add it using the AWS CLI:
aws configure --profile <profile_name>Decided you don’t need awsctx anymore? (We’ll miss you.) Here’s how to remove it:
-
Open your shell configuration file (
~/.bashrcor~/.zshrc):nano ~/.bashrc # or ~/.zshrc
-
Delete the
awsctxfunction. -
Reload your shell configuration:
source ~/.bashrc # or source ~/.zshrc
awsctxOutput:
Available profiles:
1) default
2) dev
3) prod
Current profile: dev
awsctx prodOutput:
Switched to profile: prod
That's much better than installing a new tool, right?