Last active
June 9, 2020 22:47
-
-
Save kamataryo/2ac8ddd16bc171a65604fcc5ac6f88da to your computer and use it in GitHub Desktop.
AWS 2FA CLI command. Requires Node.js >8.
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
function aws_2fa() { | |
if [[ $AWS_SESSION_TOKEN != '' ]]; then | |
echo 'You seem to have a session token already.' | |
return | |
fi | |
export MFA_SERIAL=$(aws sts get-caller-identity | \ | |
npx jqf --raw-string-output 'x => `arn:aws:iam::${x.Account}:mfa/${x.Arn.match(/^arn:aws:iam::[0-9]+:user\/(?<username>.+)$/).groups.username}`') | |
if [[ $1 != '' ]]; then | |
MFA_CODE=$1 | |
else | |
echo 'MFA code?' | |
read MFA_CODE | |
fi | |
$(aws sts get-session-token --serial-number $MFA_SERIAL --token-code $MFA_CODE | \ | |
npx jqf --raw-string-output 'x => ` | |
export AWS_ACCESS_KEY_ID=${x.Credentials.AccessKeyId} | |
export AWS_SECRET_ACCESS_KEY=${x.Credentials.SecretAccessKey} | |
export AWS_SESSION_TOKEN=${x.Credentials.SessionToken} | |
`') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment