Skip to content

Instantly share code, notes, and snippets.

@chrispruitt
Last active August 29, 2023 15:42
Show Gist options
  • Save chrispruitt/3ac46ed0597741589b15428a08279d06 to your computer and use it in GitHub Desktop.
Save chrispruitt/3ac46ed0597741589b15428a08279d06 to your computer and use it in GitHub Desktop.
awssso() {
# requires:
# - aws cli v2
if [[ -z "$AWS_CONFIG_FILE" ]]
then
local AWS_CONFIG_FILE=~/.aws/config
fi
export AWS_PROFILE=${1}
export AWS_REGION=$(cat ${AWS_CONFIG_FILE} | grep -A20 "$AWS_PROFILE" | grep sso_region | awk '{print $3}' | head -1)
export AWS_ACCOUNT=$(cat ${AWS_CONFIG_FILE} | grep -A20 "$AWS_PROFILE" | grep sso_account_id | awk '{print $3}' | head -1)
# Login only if there is no active sso session for the specified profile
aws sts get-caller-identity >/dev/null 2>&1
if [ ! $? -eq 0 ]; then
aws sso login --profile ${AWS_PROFILE}
fi
}
_awssso() {
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [[ -z "$AWS_CONFIG_FILE" ]]
then
local AWS_CONFIG_FILE=~/.aws/config
fi
WORDS="$(cat ${AWS_CONFIG_FILE} | grep "^\[profile " | sed 's/\[profile //;s/\]//')"
case "$cur" in
*)
COMPREPLY=($(compgen -W "$WORDS" -- "$cur"))
;;
esac
}
complete -F _awssso awssso
awssso-logout() {
aws sso logout --profile ${AWS_PROFILE}
unset AWS_PROFILE
unset AWS_REGION
unset AWS_ACCOUNT
}
awssso-export() {
# - requires:
# - aws-export-credentials https://github.com/benkehoe/aws-export-credentials
eval $(aws-export-credentials --env-export)
}
awssso-unset() {
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment