Skip to content

Instantly share code, notes, and snippets.

@harto
Last active August 29, 2015 14:15
Show Gist options
  • Save harto/f56157e6ed11aeb522ab to your computer and use it in GitHub Desktop.
Save harto/f56157e6ed11aeb522ab to your computer and use it in GitHub Desktop.
Ghetto AWS credential management
PROFILES_DIR="$HOME/.aws/profiles.d"
CREDENTIALS="$HOME/.aws/credentials"
aws-profile() {
local profile="$1"
if [[ -z $profile ]]; then
__aws-profile-usage >&2
else
__aws-switch-profile $profile
fi
}
__aws-profile-usage() {
echo "usage: aws-profile <profile>
Available profiles:
$(for profile in $(__aws-ls-profiles | sort); do
if [[ $profile == $(__aws-active-profile) ]]; then
echo -n " * "
else
echo -n " - "
fi
echo $profile
done)"
}
__aws-active-profile() {
basename $(readlink "$CREDENTIALS")
}
__aws-switch-profile() {
local profile_name="$1"
local profile_credentials="$PROFILES_DIR/$profile_name"
if [[ ! -f "$profile_credentials" ]]; then
echo "profile $profile_name not found" >&2
return 1
fi
ln -fhs "$profile_credentials" "$CREDENTIALS"
echo "profile $profile_name activated"
}
__aws-ls-profiles() {
ls -1 "$PROFILES_DIR" | xargs -n1 basename
}
__aws-profile-completion() {
local input="${COMP_WORDS[COMP_CWORD]}"
local candidates="$(__aws-ls-profiles | sort)"
COMPREPLY=($(compgen -W "$candidates" -- $input))
}
complete -F __aws-profile-completion aws-profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment