Skip to content

Instantly share code, notes, and snippets.

@patrykorwat
Created May 20, 2020 08:06
Show Gist options
  • Select an option

  • Save patrykorwat/91b02b7993530b8e3567c3d49c38218d to your computer and use it in GitHub Desktop.

Select an option

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
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