-
-
Save thebirk/91a71dd6c9934eda8339460f76ddd2f4 to your computer and use it in GitHub Desktop.
Zsh function for swtiching AWS profile and validating SSO sesion
This file contains hidden or 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
| awsp () { | |
| if [ -z "$1" ] | |
| then | |
| if [[ -n "$AWS_PROFILE" ]] | |
| then | |
| printf "Active profile: $AWS_PROFILE\n\n" | |
| fi | |
| local selection=$(grep "\[profile " ~/.aws/config | sed -e 's/\[profile \(.*\)\]/\1/' | fzf --height 40% --reverse --prompt="Select AWS Profile > ") | |
| [ -z "$selection" ] && return 0 | |
| set -- "$selection" | |
| fi | |
| local PROFILE=$1 | |
| export AWS_PROFILE=$PROFILE | |
| if grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox,.venv,venv} -A 10 "\[profile $PROFILE\]" ~/.aws/config | grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox,.venv,venv} -q "sso_" | |
| then | |
| echo "Checking SSO session for $PROFILE..." | |
| if ! aws sts get-caller-identity --profile "$PROFILE" > /dev/null 2>&1 | |
| then | |
| echo "⚠️ SSO session expired. Logging in..." | |
| aws sso login --profile "$PROFILE" | |
| else | |
| echo "✅ SSO session active." | |
| fi | |
| else | |
| echo "Profile '$PROFILE' set." | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment