Skip to content

Instantly share code, notes, and snippets.

@sunggun-yu
Last active May 22, 2020 04:32
Show Gist options
  • Save sunggun-yu/ce37db059be52333f0349578e1a9131a to your computer and use it in GitHub Desktop.
Save sunggun-yu/ce37db059be52333f0349578e1a9131a to your computer and use it in GitHub Desktop.
AWS MFA for assume role: works for terraform aws provider
#!/bin/bash
# prerequisites
# aws-cli : brew install awscli
# jq : brew install jq
read -p "Enter ENV (account1 | account2 | account3 | account4): " targe_env
case "$targe_env" in
"account1") aws_account=<account1>
;;
"account2") aws_account=<account2>
;;
"account3") aws_account=<account3>
;;
"account4") aws_account=<account4>
;;
esac
read -p "Enter AWS MFA Code: " mfa_code
aws_temp_credential=$(aws sts assume-role --role-arn "arn:aws:iam::${aws_account}:role/<your-role>" --role-session-name "my-role-${targe_env}" --serial-number <your-mfa-device-arn> --token-code "${mfa_code}")
export AWS_ACCESS_KEY_ID=$(echo $aws_temp_credential | jq '.Credentials.AccessKeyId'|tr -d '"')
export AWS_SECRET_ACCESS_KEY=$(echo $aws_temp_credential | jq '.Credentials.SecretAccessKey'|tr -d '"')
export AWS_SESSION_TOKEN=$(echo $aws_temp_credential | jq '.Credentials.SessionToken'|tr -d '"')
# bash -i or zsh -i
zsh -i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment