Created
May 20, 2020 08:06
-
-
Save patrykorwat/91b02b7993530b8e3567c3d49c38218d to your computer and use it in GitHub Desktop.
Assume a role with MFA in bash and export session to environmental variables
This file contains hidden or 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
| PROFILE=test | |
| echo "Please, provide MFA code:" | |
| read MFA_CODE | |
| ASSUMED_ROLE=$(aws sts assume-role --role-arn="$(aws configure get role_arn --profile $PROFILE)" --role-session-name=session --token-code=$MFA_CODE --serial-number="$(aws configure get mfa_serial --profile $PROFILE)" --output text) | |
| ASSUMED_ROLE_ARRAY=( $ASSUMED_ROLE ) | |
| if [ -z "$ASSUMED_ROLE_ARRAY" ] | |
| then | |
| echo "Error while getting token" | |
| exit 1 | |
| fi | |
| export AWS_ACCESS_KEY_ID=${ASSUMED_ROLE_ARRAY[4]} | |
| export AWS_SECRET_ACCESS_KEY=${ASSUMED_ROLE_ARRAY[6]} | |
| export AWS_SESSION_TOKEN=${ASSUMED_ROLE_ARRAY[7]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment