Last active
May 28, 2024 09:43
-
-
Save Esonhugh/a9e3ec4b10dc5800c3f2dd3f6c8f42f6 to your computer and use it in GitHub Desktop.
automatically assume role with aws cli
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
# automatically set the AWS environment variables from the json output of `aws sts assume-role` | |
aws_sts_env () { | |
if [[ -n "$1" ]] | |
then | |
local cred=$1 | |
fi | |
if [[ -z "$cred" ]] | |
then | |
echo "Usage: $0 \`json\`" | |
echo "Example: export cred=\`aws sts assume-role --role-arn xxxx --role-session-name xxxx|jq ".Credentials"\`" | |
echo " or get metadata from remote" | |
echo " export cred=\`curl 169.254.169.254/latest/meta-data/identity-credentials/ec2/security-credentials/ec2-instance\`" | |
echo " aws_sts_env '[\$cred optional]'" | |
return | |
fi | |
export AWS_ACCESS_KEY_ID=`echo $cred|jq -r '.AccessKeyId' ` | |
export AWS_SECRET_ACCESS_KEY=`echo $cred|jq -r '.SecretAccessKey'` | |
export AWS_SESSION_TOKEN=`echo $cred|jq -r '(if .SessionToken == null then .Token else .SessionToken end)'` | |
echo "SET AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN in environment." | |
unset cred | |
env | grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} AWS | awk '{ print "export " $0 }' | |
} | |
# Checkout: https://github.com/Esonhugh/WeaponizedVSCode project | |
# Usage: | |
# # normal_aws_contexnt | |
# export cred=`aws sts assume-role --role-arn xxxx --role-session-name xxxx|jq ".Credentials"` | |
# aws_sts_env | |
# # meta-data use | |
# export cred=`curl 169.254.169.254/latest/meta-data/identity-credentials/ec2/security-credentials/ec2-instance` | |
# aws_sts_env "$cred" | |
# # aws assumed role enviroment | |
# aws sts get-caller-identity | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment