Created
December 6, 2024 17:51
-
-
Save bpgould/37770c1a6b6d2dd01ad6febcfaae4904 to your computer and use it in GitHub Desktop.
zsh utility function for selecting AWS profile to perform sso login on
This file contains 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
# placed inside ~/.oh-my-zsh/custom/aliases.zsh | |
# requires fzf | |
sso() { | |
local profile_file="$HOME/.aws/config" | |
local profiles | |
# Check if ~/.aws/config exists | |
if [[ ! -f $profile_file ]]; then | |
echo "AWS config file not found: $profile_file" | |
return 1 | |
fi | |
# Extract AWS profiles and strip the '[profile ]' prefix | |
profiles=$(grep -E '^\[profile' "$profile_file" | sed -E 's/^\[profile (.*)\]/\1/') | |
# Check if any profiles were found | |
if [[ -z $profiles ]]; then | |
echo "No profiles found in $profile_file" | |
return 1 | |
fi | |
# Use fzf to select a profile | |
local profile | |
profile=$(echo "$profiles" | fzf --prompt="Select an AWS Profile: " --height=10 --border) | |
# If a profile is selected, run the aws sso login command | |
if [[ -n $profile ]]; then | |
echo "Logging in with profile: $profile" | |
aws sso login --profile "$profile" | |
else | |
echo "No profile selected." | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment