-
-
Save Wolfizen/260f9459b95eb2a96d06ef28f0bee9c2 to your computer and use it in GitHub Desktop.
AWS_PROFILE env var management
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
# MIT No Attribution | |
# | |
# Copyright 2022 Ben Kehoe | |
# 2025 Winona Schroeer-Smith | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
# software and associated documentation files (the "Software"), to deal in the Software | |
# without restriction, including without limitation the rights to use, copy, modify, | |
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# There are lots of well-built tools that completely manage your | |
# AWS profiles and login and credentials, like aws-vault and AWSume. | |
# This isn't that. | |
# I tend to prefer to go as lightweight as possible around AWS-produced tools. | |
# So all this does is set and unset your AWS_PROFILE environment variable. | |
# This code requires at least AWS CLI v2 to function correctly, because it uses the | |
# aws configure list-profiles command for validation, | |
# and this command does not exist in the AWS CLI v1. | |
# AWS CLI v2 install instructions: | |
# https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html | |
# You might also be interested in aws-whoami | |
# which improves upon `aws sts get-caller-identity` | |
# https://github.com/benkehoe/aws-whoami | |
aws-profile () { | |
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then | |
echo "USAGE:" | |
echo "aws-profile <- print out current value and available profiles" | |
echo "aws-profile PROFILE_NAME <- set PROFILE_NAME active" | |
echo "aws-profile --unset <- unset the env vars" | |
elif [ -z "$1" ]; then | |
if [ -z "$AWS_PROFILE$AWS_DEFAULT_PROFILE" ]; then | |
echo "No profile is set" | |
aws configure list-profiles | |
else | |
echo "Current profile: $AWS_PROFILE$AWS_DEFAULT_PROFILE" | |
aws configure list-profiles | grep --color "$AWS_PROFILE$AWS_DEFAULT_PROFILE\|$" | |
fi | |
elif [ "$1" = "--unset" ]; then | |
unset AWS_PROFILE | |
unset AWS_DEFAULT_PROFILE | |
else | |
if ! aws configure list-profiles | grep --color=never -Fxq -- "$1"; then | |
echo "$1 is not a valid profile" | |
return 2 | |
else | |
unset AWS_DEFAULT_PROFILE | |
export AWS_PROFILE=$1 | |
fi; | |
fi; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install by adding
. aws-profile-for-any-shell.sh
to your shell startup.The main difference from the parent script is removal of auto-completion, replacing it with a print of the current profiles on the no-option path, like how
kubectx
behaves.There are other changes to switch
export -n
tounset
. It might not actually be for any shell, I tried to remove all non-POSIX behavior but there may be something missed.The two issues mentioned in the parent script have now been resolved in AWS CLI v2.1.36 so Ive removed those comments. I kept the behaviour though because I think it is still useful.