Skip to content

Instantly share code, notes, and snippets.

@jonatw
Last active August 29, 2015 13:56
Show Gist options
  • Save jonatw/9247168 to your computer and use it in GitHub Desktop.
Save jonatw/9247168 to your computer and use it in GitHub Desktop.
this script allows ec2 instances to obtain iam role security credentials by query instance meta-data api
#!/bin/bash
function get_iam_role_credentials(){
BASE_URL='http://169.254.169.254/latest/meta-data/'
export AWS_DEFAULT_REGION=`curl -s $BASE_URL/placement/availability-zone | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`
export AWS_ROLE_NAME=`curl -s $BASE_URL/iam/security-credentials/`
export AWS_ACCESS_KEY_ID=`curl -s $BASE_URL/iam/security-credentials/$ROLE_NAME | grep AccessKeyId | awk '{print $3}' | cut -d '"' -f 2`
export AWS_SECRET_ACCESS_KEY=`curl -s $BASE_URL/iam/security-credentials/$ROLE_NAME | grep SecretAccessKey | awk '{print $3}' | cut -d '"' -f 2`
export AWS_SECURITY_TOKEN=`curl -s $BASE_URL/iam/security-credentials/$ROLE_NAME | grep Token | awk '{print $3}' | cut -d '"' -f 2`
}
get_iam_role_credentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment