Skip to content

Instantly share code, notes, and snippets.

@shollingsworth
Created May 9, 2023 17:10
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 shollingsworth/a65099d1a8b7d5bf426af5fbd70de55f to your computer and use it in GitHub Desktop.
Save shollingsworth/a65099d1a8b7d5bf426af5fbd70de55f to your computer and use it in GitHub Desktop.
bash source file for using yawsso and aws sso login to set your current environment to make aws sso transparent with whatever tooling you are using
#!/usr/bin/env bash
# install yawsso https://github.com/victorskl/yawsso
# make sure aws cli is installed
# call with `sso`
################################################################################
# when calling these functions make sure AWS_PROFILE is set
################################################################################
SSO_REGION="us-east-2"
awswhoami() {
out=$(aws sts get-caller-identity | jq .)
echo "${out}"
arn=$(echo "${out}" | jq -r '.Arn')
name=$(echo "${arn}" | tr '/' ' ' | awk '{print $2}')
acctnum=$(echo "${arn}" | tr ':' ' ' | awk '{print $4}')
aalias=$(aws iam list-account-aliases | jq ".AccountAliases[0]" -r)
# if [[ "*AWSReservedSSO*" "${arn}" ]]; then
if echo "${arn}" | grep -q 'AWSReservedSSO'; then
out="Role: arn:aws:iam::${acctnum}:role/aws-reserved/sso.amazonaws.com/${SSO_REGION}/${name}"
else
out="Role: arn:aws:iam::${acctnum}:role/${name}"
fi
echo "${out}"
echo "Account: ${aalias}"
}
checkawsprofile() {
if [[ -z "${AWS_PROFILE}" ]]; then
echo "AWS_PROFILE is not set"
return 1
fi
return 0
}
alias yawme="checkawsprofile && yawsso -p ${AWS_PROFILE}"
alias sso="checkawsprofile && aws sso login && yawme && awswhoami"
@twl0ng
Copy link

twl0ng commented May 9, 2023

noice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment