AWS Azure login interactive profile selection function
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 underlyingaws-ozure-loginseamlessly.
- Lets you select from profiles without a source_profile.
- Logs in by running the original aws-azure-logincommand 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_PROFILEafter logging in. This is necessary at work, where we use sub-profiles to assume a different role from the login step.
Dependencies
- aws-azure-login
- Bash
- RipGrep (but feel free to convert it to use GNU Grep)
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
- Copy the function into your ~/.bashrc.
- Restart your shell.
Usage
Run aws-azure-login and follow the instructions.
No webmentions were found.