-
Notifications
You must be signed in to change notification settings - Fork 42
AWS Assume Role script improvements #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
letic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to assign There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Uh oh!
There was an error while loading. Please reload this page.