Skip to content

Instantly share code, notes, and snippets.

@shollingsworth
Created May 9, 2023 17:10
Show Gist options
  • Save shollingsworth/a65099d1a8b7d5bf426af5fbd70de55f to your computer and use it in GitHub Desktop.
Save shollingsworth/a65099d1a8b7d5bf426af5fbd70de55f to your computer and use it in GitHub Desktop.

Revisions

  1. shollingsworth created this gist May 9, 2023.
    39 changes: 39 additions & 0 deletions awssso.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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"