Skip to content

Instantly share code, notes, and snippets.

@odessky
Forked from fgassert/ec2-get-security-credentials
Last active March 18, 2020 01:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odessky/e7b8b31e3b710cf3e29e85339d4ec5e6 to your computer and use it in GitHub Desktop.
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.
#!/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