-
-
Save vbogretsov/b7b6a34d191954f7bc1d4be89c9da530 to your computer and use it in GitHub Desktop.
Assume AWS 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
| role=$1 | |
| if ! [ $role ]; then | |
| echo "error: [sts] missing required argument ROLE" | |
| exit 1 | |
| fi | |
| account_id=$(aws sts get-caller-identity | jq .Account | sed s/\"//g) | |
| if ! [ $account_id ]; then | |
| echo "error: unable to get AWS account ID" | |
| exit 1 | |
| fi | |
| aws sts assume-role \ | |
| --role-arn arn:aws:iam::${account_id}:role/${role} \ | |
| --role-session-name AWSCLI-Session \ | |
| | jq '.Credentials|[.AccessKeyId, .SecretAccessKey, .SessionToken]' \ | |
| | sed 's/,//g' \ | |
| | sed 's/\[//g' \ | |
| | sed 's/\]//g' \ | |
| | sed 's/\"//g' \ | |
| | awk 'NF' \ | |
| | awk 'BEGIN { \ | |
| names[0]="AWS_ACCESS_KEY_ID"; \ | |
| names[1]="AWS_SECRET_ACCESS_KEY"; \ | |
| names[2]="AWS_SESSION_TOKEN"; \ | |
| } { print names[NR-1]"="$1 }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment