Last active
December 23, 2021 04:02
-
-
Save arledesma/fd4e0fb6d83937f422beef50a31f509c to your computer and use it in GitHub Desktop.
generated functions for logging into aws organizations accounts
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
# this file should be sourced in your .bashrc or .zshrc | |
# aws-cli-login.sh is from infrastructure-live-customer/.tools/aws/ | |
function sso-assume-role { | |
# support both bash and zsh function stack reference to get the calling function name | |
local -r profile="${FUNCNAME[-1]:-${funcstack[-1]}}"; | |
pushd "$(dirname "$(command -v aws-cli-login.sh)")" &>/dev/null; | |
[ -n "$1" ] && source aws-cli-login.sh --sso --profile "${profile//-legacy/}" "$1"; | |
[ -z "$1" ] && source aws-cli-login.sh --sso --profile "${profile//-legacy/}"; | |
popd &>/dev/null; | |
} | |
# reads the profiles that are configured within ~/.aws/config and adds a function named for each profile | |
# wrapping the sso-assume-role function | |
for func in $(sed -n -e '/^\[profile /{ s/^\[profile \([^] ]\+\)\]/\1/p }' ~/.aws/config); do | |
if [ "$func" = "default" ]; then continue; fi | |
eval "function $func { sso-assume-role --use-sso-util; }"; | |
eval "function ${func}-legacy { sso-assume-role; }"; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment