Last active
October 23, 2025 15:16
-
-
Save keif/7e2a1282af092f30724d975f75e9a0bb to your computer and use it in GitHub Desktop.
A .zshrc helper for `gimme-aws-creds` with matching region to account, and error handling for default profiles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # aws | |
| alias gac="gimme-aws-creds" | |
| # Unset existing AWS credentials | |
| unset_aws() { | |
| for i in AWS_ACCESS_KEY_ID AWS_DEFAULT_PROFILE AWS_DEFAULT_REGION AWS_REGION AWS_SECRET_ACCESS_KEY AWS_SECURITY_TOKEN AWS_SESSION_TOKEN AWS_SHARED_CREDENTIALS_FILE AWS_VAULT; do unset $i; done | |
| } | |
| function assume { | |
| # Declare a map of AWS_PROFILE to AWS_REGION | |
| declare -A profile_regions=( | |
| [profile1/account1]=<REGION_PLACEHOLDER> | |
| [profile2/account2]=<REGION_PLACEHOLDER> | |
| …etc. | |
| ) | |
| # Unset existing AWS credentials | |
| unset_aws | |
| # Select AWS profile using fzf | |
| if ! command -v fzf &> /dev/null; then | |
| echo "fzf not found. Please install fzf to use this function." | |
| return 1 | |
| fi | |
| export AWS_PROFILE=$(aws configure list-profiles | sort | fzf --tac) && echo Switched to $AWS_PROFILE! | |
| export AWS_DEFAULT_PROFILE=$AWS_PROFILE | |
| export AWS_SHARED_CREDENTIALS_FILE="${HOME}/.aws/credentials" | |
| if [[ -v profile_regions[$AWS_PROFILE] ]]; then | |
| region="$profile_regions[$AWS_PROFILE]" | |
| else | |
| region="us-west-2" | |
| fi | |
| echo "Setting region to ${region}" | |
| export AWS_REGION=${region} | |
| export AWS_DEFAULT_REGION=${region} | |
| } | |
| # better gimme-access-creds | |
| function gac() { | |
| if [ -z "${AWS_PROFILE:-}" ]; then | |
| echo "[gac] No AWS_PROFILE set — defaulting to 'default'" | |
| export AWS_PROFILE=default | |
| elif [ "${AWS_PROFILE}" = "()" ]; then | |
| echo "[gac] AWS_PROFILE is invalid (empty parentheses) — defaulting to 'default'" | |
| export AWS_PROFILE=default | |
| fi | |
| command gimme-aws-creds "$@" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment