Created
March 11, 2020 11:42
-
-
Save novice81/0f71f63ac586377fe38312218805aa0a to your computer and use it in GitHub Desktop.
AWS CLI to assume role
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| $PROFILE_NAME="user-has-assume-role" | |
| $ROLE_ARN="role-arn-that-has-policies-to-work" | |
| $SESSION_NAME="session-name-to-identify" | |
| export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile $PROFILE_NAME) | |
| export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile $PROFILE_NAME) | |
| unset AWS_SESSION_TOKEN | |
| ASSUME_ROLE_RESULT=$(aws sts assume-role \ | |
| --role-arn $ROLE_ARN \ | |
| --role-session-name $SESSION_NAME) | |
| export AWS_ACCESS_KEY_ID=$(echo $ASSUME_ROLE_RESULT | jq -r '.Credentials.AccessKeyId') | |
| export AWS_SECRET_ACCESS_KEY=$(echo $ASSUME_ROLE_RESULT | jq -r '.Credentials.SecretAccessKey') | |
| export AWS_SESSION_TOKEN=$(echo $ASSUME_ROLE_RESULT | jq -r '.Credentials.SessionToken') | |
| aws sts get-caller-identity | |
| # You can use this like '$ source assume-role.sh'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment