Forked from fgassert/ec2-get-security-credentials
Last active
March 18, 2020 01:49
-
-
Save odessky/e7b8b31e3b710cf3e29e85339d4ec5e6 to your computer and use it in GitHub Desktop.
Gets IAM Role Security Credentials from instance metadata and export they in bash session envirovment. Secured edition.
This file contains 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 | |
export-aws () { | |
# gets iam security credentials from instance metadata and export in envirovment for runtime script | |
# after script finished credentials is gone away | |
# system must have awscli&jq preinstalled | |
# converted to function | |
# Usage: export-aws ROLENAME | |
if [ -n "$1" ] ; then | |
# get security credentials from instance metadata into CR var | |
CR=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/$1/) | |
export AWS_ACCESS_KEY_ID=$(echo $CR | jq -r '.AccessKeyId') | |
export AWS_SECRET_ACCESS_KEY=$(echo $CR | jq -r '.SecretAccessKey') | |
export AWS_SESSION_TOKEN=$(echo $CR | jq -r '.Token') | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment