Supercharge Your AWS CLI: Add kubectx-Style Tab Autocompletion for Profile Switching
Share this article
For developers and DevOps engineers managing multiple AWS environments, switching profiles via export AWS_PROFILE=... is a notorious productivity killer. Mistyped profile names, context-checking overhead, and disrupted workflows plague daily operations—especially when compared to Kubernetes' efficient kubectx tool. What if you could bring that same fluidity to AWS? Here's how to implement a custom awsctx command with intelligent tab autocompletion in Zsh, turning tedious profile switches into a seamless experience.
The kubectx Inspiration
Tools like kubectx revolutionized Kubernetes context management by combining fuzzy search (via fzf) and tab completion. This approach eliminates memorization and reduces errors—critical when handling production environments. By adapting this pattern for AWS, we create muscle-memory efficiency: type awsctx <Tab> to autocomplete profiles or press Enter alone to trigger an interactive selector.
Step-by-Step Implementation
1. Define the awsctx Function
Add this to ~/.zshrc. It uses fzf for interactive selection when no argument is provided:
function awsctx() {
if [ -z "$1" ]; then
profile=$(aws configure list-profiles | fzf)
if [[ $profile != "" ]]; then
export AWS_PROFILE=$profile
fi
else
export AWS_PROFILE=$1
fi
echo -e "\033[32m$AWS_PROFILE\033[0m selected"
}
The green confirmation output ensures visual feedback, critical in fast-paced terminal sessions.
2. Enable Bash Completion in Zsh
Zsh lacks native support for Bash-style completion. Bridge the gap by adding these lines before sourcing your completion script:
autoload -Uz compinit && compinit
autoload -Uz bashcompinit && bashcompinit
3. Create the Autocompletion Script
Save this as ~/.zsh/completions/_awsctx_completion and make it executable (chmod +x):
#!/usr/bin/env bash
_awsctx_completion() {
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi
local profiles=($(compgen -W "$(aws configure list-profiles)" "${COMP_WORDS[1]}"))
if [ "${#profiles[@]}" == "1" ]; then
local profile=$(echo ${profiles[0]/%\ */})
COMPREPLY=("$profile")
else
COMPREPLY=("${profiles[@]}")
fi
}
complete -F _awsctx_completion awsctx
This script dynamically fetches profiles using aws configure list-profiles and leverages compgen to filter matches based on user input. The COMP_WORDS array intelligently handles argument position.
4. Source the Script
Add this to ~/.zshrc after enabling completions:
source $HOME/.zsh/completions/_awsctx_completion
5. Test and Iterate
Reload your configuration (source ~/.zshrc) and type awsctx followed by Tab. Available profiles populate instantly. For ad-hoc testing, combine with aws sts get-caller-identity to verify context.
Why This Matters for Cloud Workflows
Tab completion isn’t just convenience—it’s risk reduction. Manual profile switches invite typos that can accidentally deploy to production or breach security boundaries. By automating suggestions:
- Speed ⚡: Reduce switch time from seconds to keystrokes.
- Accuracy 🎯: Eliminate misconfigured profile errors.
- Ergonomics 💻: Maintain flow state during complex multi-account operations.
Pro Tips for Scale
- Plugin Management: Use Oh My Zsh or zinit to compartmentalize the function and completion script, easing updates.
- Cross-Shell Portability: Abstract the completion logic into a standalone script compatible with Bash via
complete -C. - Context-Aware Automation: Pair with
direnvto auto-switch profiles based on project directories, creating a true Kubernetes-style experience.
Embracing patterns from tools like kubectx showcases the AWS CLI’s extensibility. This setup isn’t just a hack—it’s a strategic upgrade for any team juggling cloud environments. Implement it once, and you’ll wonder how you ever worked without it.