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 $(
        rg --multiline --only-matching --pcre2 '(?x)
        (?<=^\[profile\ ) # Profile line prefix
        ([^\]]+) # Capture profile name
        (?=\]$) # Profile line suffix
        (?!\]\nsource_profile=) # Profile does *not* have a parent
        ' ~/.aws/config
    ); do
        export AWS_PROFILE
        break
    done

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

    local sub_profiles
    mapfile -t sub_profiles < <(
        rg --multiline --only-matching --pcre2 '(?x)
            (?<=^\[profile\ ) # Profile line prefix
            ([^\]]+) # Capture profile name
            (?=\]$) # Profile line suffix
            (?=\]\nsource_profile= # Profile *does* have a parent …
            '"$AWS_PROFILE"') # … with the given name
            ' ~/.aws/config
    )

    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.