Skip to content

Instantly share code, notes, and snippets.

@idooo
Created December 15, 2017 00:35
Show Gist options
  • Save idooo/b579229bfe29c97594c54ae0fa94adf9 to your computer and use it in GitHub Desktop.
Save idooo/b579229bfe29c97594c54ae0fa94adf9 to your computer and use it in GitHub Desktop.
STS Assume role script helper
#! /bin/bash
# Requirements:
# brew install jq
#
# Launch:
# Executing script will print environment variables (eg "~/src/assume-role.sh 983056893078 OrganizationAccountAccessRole master")
# Use "." in bash if you want to export them to a current session
# (eg: . ~/src/assume-role.sh 12312312313 OrganizationAccountAccessRole master)
unset AWS_SESSION_TOKEN
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
ACCOUNT_ID=$1
ROLE=$2
PROFILE=$3
HOSTNAME=$(hostname)
temp_role=$(aws sts assume-role \
--role-arn "arn:aws:iam::$ACCOUNT_ID:role/$ROLE" \
--role-session-name "$HOSTNAME-$ROLE" \
--profile $PROFILE)
export AWS_ACCESS_KEY_ID=$(echo $temp_role | jq .Credentials.AccessKeyId | xargs)
export AWS_SECRET_ACCESS_KEY=$(echo $temp_role | jq .Credentials.SecretAccessKey | xargs)
export AWS_SESSION_TOKEN=$(echo $temp_role | jq .Credentials.SessionToken | xargs)
env | grep -i AWS_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment