Skip to content

Instantly share code, notes, and snippets.

@iley
Created January 17, 2020 09:46
Show Gist options
  • Save iley/805fbfb6772c7b7ad3d084c59bdfef80 to your computer and use it in GitHub Desktop.
Save iley/805fbfb6772c7b7ad3d084c59bdfef80 to your computer and use it in GitHub Desktop.
Script to switch AWS between profiles configured with SSO
#!/bin/bash
set -e
new_profile="$1"
if [[ -z "$new_profile" ]]; then
# Just list existing profiles.
profiles=$(cat ~/.aws/config | perl -ne 'print if s/\[profile\s+(\S+)\]/$1/')
default_account=$(aws configure get sso_account_id --profile default)
for profile in $profiles; do
profile_account=$(aws configure get sso_account_id --profile $profile)
if [[ $default_account == $profile_account ]]; then
echo " * $profile"
else
echo " $profile"
fi
done
else
profile_account=$(aws configure get sso_account_id --profile $new_profile)
aws --profile default configure set sso_account_id $profile_account
echo "Default profile is set to $new_profile"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment