-
-
Save egoens/3163702351dd800727d8730770bd8f6e to your computer and use it in GitHub Desktop.
Export AWS key variables based on profile name
This file contains 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
# Shortcut for assigning a heredoc to a variable. | |
# http://stackoverflow.com/a/8088167 | |
define() { | |
# read will have exit status 1 whenever it assigns to a variable or it gets to EOF. | |
read -r -d '' "${1}" || true | |
} | |
_other_aws_common_stuff() { | |
export AWS_DEFAULT_PROFILE="${AWS_PROFILE}" | |
local SCRIPT | |
unset AWS_ACCESS_KEY_ID | |
unset AWS_SECRET_ACCESS_KEY | |
define SCRIPT <<EOF | |
try: | |
# Prefer boto3 | |
from botocore.session import Session | |
s = Session() | |
c = s.get_credentials() | |
print('export AWS_ACCESS_KEY_ID=' + c.access_key) | |
print('export AWS_SECRET_ACCESS_KEY=' + c.secret_key) | |
except ImportError as e: | |
# Try boto if boto3 isn't available. | |
from boto.provider import Provider | |
p = Provider('aws') | |
print('export AWS_ACCESS_KEY_ID=' + p.get_access_key()) | |
print('export AWS_SECRET_ACCESS_KEY=' + p.get_secret_key()) | |
EOF | |
local RESULT | |
RESULT=$(echo "${SCRIPT}" | python) | |
eval "${RESULT}" | |
} | |
aws_unizin() { | |
export AWS_PROFILE=unizin | |
_other_aws_common_stuff | |
} | |
aws_cl() { | |
export AWS_PROFILE=courseload | |
_other_aws_common_stuff | |
} | |
aws_mine() { | |
export AWS_PROFILE=personal | |
_other_aws_common_stuff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment