Skip to content

Instantly share code, notes, and snippets.

@arledesma
Last active December 23, 2021 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arledesma/fd4e0fb6d83937f422beef50a31f509c to your computer and use it in GitHub Desktop.
Save arledesma/fd4e0fb6d83937f422beef50a31f509c to your computer and use it in GitHub Desktop.
generated functions for logging into aws organizations accounts
# 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