Skip to content

Instantly share code, notes, and snippets.

@ksato9700
Last active November 30, 2020 05:57
Show Gist options
  • Save ksato9700/d26d9c62702fbda17f0a45ea616d13e5 to your computer and use it in GitHub Desktop.
Save ksato9700/d26d9c62702fbda17f0a45ea616d13e5 to your computer and use it in GitHub Desktop.
function update-awscli-mfa() {
if [ "x$1" = "x" ]; then
echo "update-awscli-mfa <mfa code>"
return 1
fi
device=$(sed -n '/^\[mfa/,/^\[/p' ~/.aws/credentials|awk '$1=="aws_mfa_device"{print $3}')
if [ "x$device" = "x" ]; then
echo "no mfa profile or aws_mfa_device found in ~/.aws/credentials"
return 2
fi
eval $(aws sts get-session-token --profile mfa --serial-number $device --token-code $1 | awk '$1 == "\"AccessKeyId\":" { gsub(/\"/,""); gsub(/,/,""); print "export AWS_ACCESS_KEY_ID="$2 } $1 == "\"SecretAccessKey\":" { gsub(/\"/,""); gsub(/,/,""); print "export AWS_SECRET_ACCESS_KEY="$2 } $1 == "\"SessionToken\":" { gsub(/\"/,""); gsub(/,/,""); print "export AWS_SESSION_TOKEN="$2 } ')
aws configure list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment