Skip to content

Instantly share code, notes, and snippets.

@jamesharr
Created August 6, 2020 15:40
Show Gist options
  • Save jamesharr/905b52e9c18bd6e533416b33ca2229d7 to your computer and use it in GitHub Desktop.
Save jamesharr/905b52e9c18bd6e533416b33ca2229d7 to your computer and use it in GitHub Desktop.
aws-token(){
aws_token_sn=arn:aws:iam::REDACTED_ACCT_NUM:mfa/REDACTED_USER_PASSWD
aws_token_duration=43200
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
export AWS_SESSION_TOKEN=""
read -p "MFA Code: " aws_token_code
OUT="$(aws sts get-session-token \
--serial-number $aws_token_sn \
--token-code $aws_token_code \
--duration-seconds $aws_token_duration \
--output text
)"
if [ $? -ne 0 ]; then
echo "ERROR: $OUT"
return
else
export AWS_ACCESS_KEY_ID="$(echo "$OUT" | awk '{print $2}')"
export AWS_SECRET_ACCESS_KEY="$(echo "$OUT" | awk '{print $4}')"
export AWS_SESSION_TOKEN="$(echo "$OUT" | awk '{print $5}')"
expire="$(echo "$OUT" | awk '{print $3}')"
echo "Temporary key applied and will expire $expire"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment