Last active
July 11, 2019 04:37
-
-
Save masayoshi634/129757bb1f97fe0213b0206fe64b02af to your computer and use it in GitHub Desktop.
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/sh | |
AWS_PROFILE=${AWS_PROFILE:="default"} | |
AWS_ROLE_SESSION_NAME=$(date +%Y%m%d%H%M%S-session) | |
DURATION="7200" | |
print_help() { | |
cat <<-EOF >> /dev/stdout | |
Get and Set AWS_SESSION_TOKEN Environment | |
Depends: | |
jq (https://stedolan.github.io/jq/) | |
Usage: $0 [-p AWS_PROFILE] [-d DURATION] | |
Options: | |
-p [AWS_PROFILE] AWS Profile | |
-d [DURATION] The duration, in seconds, of the role session | |
-h print this message and exit | |
Example: source $0 -p test-account && env | grep AWS | |
EOF | |
} | |
while getopts "p:d:h" opt; do | |
case ${opt} in | |
p ) AWS_PROFILE=${OPTARG} ;; | |
d ) DURATION=${OPTARG} ;; | |
h ) print_help; exit ;; | |
* ) print_help; exit 1 ;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
ROLE_ARN=$(aws --profile ${AWS_PROFILE} configure get role_arn) | |
TEMP_ROLE=$(aws --profile ${AWS_PROFILE} sts assume-role --role-arn ${ROLE_ARN} --role-session-name ${AWS_ROLE_SESSION_NAME} --duration-seconds ${DURATION}) | |
export AWS_ACCESS_KEY_ID=$(echo ${TEMP_ROLE} | jq -r '.Credentials.AccessKeyId') | |
export AWS_SECRET_ACCESS_KEY=$(echo ${TEMP_ROLE} | jq -r '.Credentials.SecretAccessKey') | |
export AWS_SESSION_TOKEN=$(echo ${TEMP_ROLE} | jq -r '.Credentials.SessionToken') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment