aws-sts-get-session-token script

From wikieduonline
Jump to navigation Jump to search

Usage

aws-sts-get-session-token yourusername your_token

Code

#/bin/bash
MYUSERNAME=$1
MYTOKEN=$2

function usage() {
    echo "Usage: $(basename $0) your_username your_token"
    exit 0
}

[ -z $1 ] && { usage; }

MFA_DEVICE=$(aws iam list-virtual-mfa-devices --output text | grep $MYUSERNAME | grep VIRTUALMFADEVICES | awk '{print $3}')
output=$(aws sts get-session-token --serial-number $MFA_DEVICE --token-code $MYTOKEN --output text)

AWS_ACCESS_KEY_ID=$(echo $output | awk '{print $2}')
AWS_SECRET_ACCESS_KEY=$(echo $output | awk '{print $4}')
AWS_SESSION_TOKEN=$(echo $output | awk '{print $NF}') 

echo "export AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID"" 
echo "export AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY""
echo "export AWS_SESSION_TOKEN="$AWS_SESSION_TOKEN""

Related

See also

Advertising: