Skip to content

Instantly share code, notes, and snippets.

@schen1628
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save schen1628/d5c3416819b26ce89c48 to your computer and use it in GitHub Desktop.

Select an option

Save schen1628/d5c3416819b26ce89c48 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is to set AWS envrionments with temporary security tokens
# It takes three position arguments that are used by aws sts assume-role:
# The first position: Role ARN
# The second position: External ID
# The third position: Role Session Name
#
# For example: /aws-sts-set-env.sh arn:aws:iam::012345678901:role/my-role my-external-id my-session-name
#
unset AWS_SESSION_TOKEN
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
ROLE_ARN=$1
EXTERNAL_ID=$2
ROLE_SESSION_NAME=$3
TIMESTAMP=$(date +"%Y%m%d%H%M")
OUTFILE=/tmp/aws-sts-set-env$TIMESTAMP.out
aws sts assume-role --role-arn $ROLE_ARN --external-id $EXTERNAL_ID --role-session-name $ROLE_SESSION_NAME > $OUTFILE
export AWS_ACCESS_KEY_ID=`grep -o "\"AccessKeyId\": \".*\"" $OUTFILE | cut -d':' -f2 | sed -e 's/^\s"//' -e 's/"$//'`
export AWS_SECRET_ACCESS_KEY=`grep -o "\"SecretAccessKey\": \".*\"" $OUTFILE | cut -d':' -f2 | sed -e 's/^\s"//' -e 's/"$//'`
export AWS_SESSION_TOKEN=`grep -o "\"SessionToken\": \".*\"" $OUTFILE | cut -d':' -f2 | sed -e 's/^\s"//' -e 's/"$//'`
rm -f $OUTFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment