Skip to content

Instantly share code, notes, and snippets.

@nazartm
Created June 17, 2019 08:59
Show Gist options
  • Save nazartm/693c31f46df5af49ce1fbcc2e98b45f5 to your computer and use it in GitHub Desktop.
Save nazartm/693c31f46df5af49ce1fbcc2e98b45f5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# usage `source assume_role.sh prod token`
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
if (( $# != 2 )); then
echo "Two arguments are required: profile and token"
exit 1
fi
PROFILE="$1"
TOKEN="$2"
case "$PROFILE" in
prod)
ACCOUNT="123887191192:role/admin"
;;
stage)
ACCOUNT="123219521568:role/admin"
;;
dev)
ACCOUNT="123610647686:role/admin"
;;
*)
echo "Usage: profile and token."
esac
export AWS_PROFILE=$PROFILE
export AWS_DEFAULT_PROFILE=iam
export AWS_REGION=eu-west-1
export AWS_DEFAULT_REGION=eu-west-1
echo $ACCOUNT
credentials=$(aws sts assume-role --role-arn arn:aws:iam::$ACCOUNT --role-session-name nazar-annagurban-$PROFILE-session --serial-number arn:aws:iam::491473123123:mfa/nazar.annagurban --token-code $TOKEN )
export AWS_ACCESS_KEY_ID=$(echo $credentials | jq -r '.Credentials.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(echo $credentials | jq -r '.Credentials.SecretAccessKey')
export AWS_SESSION_TOKEN=$(echo $credentials | jq -r '.Credentials.SessionToken')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment