Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions aws-cli-assumerole.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
#! /bin/bash
#
# Dependencies:
# brew install jq
# MacOS : brew install jq
# Linux (deb based) : apt install jq
#
# Setup:
# chmod +x ./aws-cli-assumerole.sh
#
# Execute:
# source ./aws-cli-assumerole.sh
# source ./aws-cli-assumerole.sh <AWS_ID> [<AWS_ROLE> [<SESSION_NAME>]]
#
# Description:
# Makes assuming an AWS IAM role (+ exporting new temp keys) easier

unset AWS_SESSION_TOKEN
export AWS_ACCESS_KEY_ID=<user_access_key>
export AWS_SECRET_ACCESS_KEY=<user_secret_key>
export AWS_REGION=eu-west-1
AWS_PROFILE="default"
AWS_ROLE="OrganizationAccountAccessRole"

temp_role=$(aws sts assume-role \
--role-arn "arn:aws:iam::<aws_account_number>:role/<role_name>" \
--role-session-name "<some_session_name>")
if [ $# -eq 0 ] || [ $# -gt 3 ]; then
echo "Usage : $0 <AWS_ID> [<AWS_ROLE> <SESSION_NAME>]"
else
if [ ! -z "$2" ]; then
AWS_ROLE=$2
fi
if [ ! -z "$3" ]; then
AWS_SESSION_NAME=$3
else
AWS_SESSION_NAME="Assume-$1"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to assign $1 to a variable (e.g. AWS_ID) just for clarity, especially later where you reference $1 as part of the assume role line (it makes that line just a little bit clearer).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure good idea 👍

I also thought that we should add a parameter to revert the credentials to the default AWS profile.

Looking forward to your improvements.

fi

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)
unset AWS_SESSION_TOKEN
export AWS_ACCESS_KEY_ID=$(grep -A2 "\[$AWS_PROFILE\]" ~/.aws/credentials | awk -F"= " '/aws_access_key_id/ {print $2}')
export AWS_SECRET_ACCESS_KEY=$(grep -A2 "\[$AWS_PROFILE\]" ~/.aws/credentials | awk -F"= " '/aws_secret_access_key/ {print $2}')
export AWS_REGION=$(grep -A2 "\[$AWS_PROFILE\]" ~/.aws/config | awk -F"= " '/region/ {print $2}')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the grep/awk flows are effectively the same, would it make sense to move the logic to a function and just pass in aws_access_key_id, aws_secret_access_key, region as the differentiator?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course that would be cleaner.


env | grep -i AWS_
TEMP_ROLE=$(aws sts assume-role \
--role-arn "arn:aws:iam::$1:role/$AWS_ROLE" \
--role-session-name "$AWS_SESSION_NAME")

echo "Assumed ARN : $(echo $TEMP_ROLE | jq .AssumedRoleUser.Arn | xargs)"

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