Skip to content

Instantly share code, notes, and snippets.

@dotarr
Last active May 13, 2021 10:09
Show Gist options
  • Save dotarr/f3053ce91e1990d5be0b161b21285e1e to your computer and use it in GitHub Desktop.
Save dotarr/f3053ce91e1990d5be0b161b21285e1e to your computer and use it in GitHub Desktop.
Interactive AWS profile selector
#!/bin/bash
CONFIG_FILE="${HOME}/.aws/config"
function awsprofile() {
if [ -z ${AWS_PROFILE+x} ]; then msg="AWS_PROFILE is unset"; else msg="Current AWS_PROFILE=${AWS_PROFILE}"; fi
echo -e "\n${msg}"
echo -e "Available profiles:\n"
PS3=$'\n'"Use number to select, 'u' to unset or 'q' to quit: "
select profile in $(awk '/^\[/ { gsub("]", ""); print $2 }' <${CONFIG_FILE}); do
if [[ "${REPLY}" == q ]]; then break; fi
if [[ "${REPLY}" == u ]]; then
unset AWS_PROFILE
echo "AWS_PROFILE unset"
break
fi
if [[ "${profile}" == "" ]]; then
echo "'${REPLY}' is not a valid number"
continue
fi
export AWS_PROFILE=${profile}
echo "AWS_PROFILE set to ${AWS_PROFILE}"
break
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment