Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Executes aws sts get-session-token, and sets the result to .aws/credentials
#!/bin/bash
# Based on the following shell script:
# https://github.com/DevNambi/utility-scripts/blob/master/sessioner.sh
#
echo $@
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--serial)
SERIAL="$2"
shift # past argument
shift # past value
;;
-p|--profile)
PROFILE="$2"
shift # past argument
shift # past value
;;
-t|--token)
TOKEN="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
echo "Configuring $PROFILE with token $TOKEN"
CREDJSON="$(aws sts get-session-token --duration 129600 --serial-number $SERIAL --profile $PROFILE --token-code $TOKEN)"
ACCESSKEY="$(echo $CREDJSON | jq '.Credentials.AccessKeyId' | sed 's/"//g')"
SECRETKEY="$(echo $CREDJSON | jq '.Credentials.SecretAccessKey' | sed 's/"//g')"
SESSIONTOKEN="$(echo $CREDJSON | jq '.Credentials.SessionToken' | sed 's/"//g')"
PROFILENAME=${PROFILE}-session
echo "Profile $PROFILENAME AccessKey $ACCESSKEY SecretKey $SECRETKEY"
echo "SessionToken $SESSIONTOKEN"
aws configure set aws_access_key_id $ACCESSKEY --profile $PROFILENAME
aws configure set aws_secret_access_key $SECRETKEY --profile $PROFILENAME
aws configure set aws_session_token $SESSIONTOKEN --profile $PROFILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment