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
#!/bin/bash | |
# Tom R. | |
# based on https://gist.github.com/dukejones/36128decdb1e003ac5d77f5c4523f1f5 | |
# Changed to use ~/.aws/config instead | |
# | |
set -e | |
usage () { | |
cat <<DOCUMENTATIONXX | |
Usage : $0 PROFILE_NAME | |
This version just prints the token ready to be exported | |
Examples | |
eval \$($0 Profile) | |
DOCUMENTATIONXX | |
} | |
if [ "$1" == "-h" -o "$1" == "--help" -o "$1" == "" ]; then | |
usage | |
exit 1 | |
fi | |
source_profile_name=$1 | |
role_arn=$(cat $HOME/.aws/config | grep -A 1 "$source_profile_name\]" | tail -n 1 | sed 's/role_arn = //') | |
session_name="${USER}-`hostname`-`date +%Y%m%d`" | |
sts=( $( | |
aws sts assume-role \ | |
--role-arn "$role_arn" \ | |
--role-session-name "$session_name" \ | |
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \ | |
--output text | |
) ) | |
echo "export AWS_ACCESS_KEY_ID=${sts[0]} AWS_SECRET_ACCESS_KEY=${sts[1]} AWS_SESSION_TOKEN=${sts[2]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment