Skip to content

Instantly share code, notes, and snippets.

@conleym
Last active November 2, 2016 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save conleym/df0a87e51187ba82d18e to your computer and use it in GitHub Desktop.
Save conleym/df0a87e51187ba82d18e to your computer and use it in GitHub Desktop.
Export AWS key variables based on profile name
# 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
}
@conleym
Copy link
Author

conleym commented May 4, 2016

I have awscli installed via pip install --user awscli.

In ~/.aws/config,

[profile unizin]
output = json
region = us-west-2


[profile courseload]
output = json
region = us-east-1


[profile personal]
output = json
region = us-west-2

Matching credentials are set up in ~/.aws/credentials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment