I’ve created a wrapper for aws-azure-login to let you interactively select the profile you want before logging in. I have a bunch of profiles, I can never remember what they are called, and some of them have short session lifetimes, so this has been a lifesaver.

Features

  • Uses local variables to avoid polluting the namespace.
  • Forwards any extra arguments (such as --no-prompt) to the underlying aws-ozure-login seamlessly.
  • Lets you select from profiles without a source_profile.
  • Logs in by running the original aws-azure-login command under the hood.
  • Sets AWS_PROFILE.
  • If there are any sub-profiles of the selected profile, it lets you select from them to set AWS_PROFILE after logging in. This is necessary at work, where we use sub-profiles to assume a different role from the login step.

Dependencies

The function

aws-azure-login() {
    echo "Select profile, or press Ctrl-c to cancel."
    select AWS_PROFILE in $(
        grep --null-data --only-matching --perl-regexp --regex='(?<=\[profile )([^\]]+)(?=\])(?!\]\nsource_profile=)' ~/.aws/config |
            tr '\0' '\n'
    ); do
        export AWS_PROFILE
        break
    done

    command aws-azure-login --profile="$AWS_PROFILE" "$@"

    local sub_profiles
    mapfile -t sub_profiles < <(
        grep --null-data --only-matching --perl-regexp --regex='(?<=\[profile )([^\]]+)(?=\]\nsource_profile='"$AWS_PROFILE"'\n)' ~/.aws/config |
            tr '\0' '\n'
    )

    if (("${#sub_profiles[@]}" != 0)); then
        echo "Select sub-profile, or press Ctrl-c to keep ${AWS_PROFILE}."
        select AWS_PROFILE in "${sub_profiles[@]}"; do
            break
        done
    fi
}

Setup

  1. Copy the function into your ~/.bashrc.
  2. Restart your shell.

Usage

Run aws-azure-login and follow the instructions.