Skip to content

Instantly share code, notes, and snippets.

@kll
Last active May 15, 2018 02:18
Show Gist options
  • Save kll/7800160 to your computer and use it in GitHub Desktop.
Save kll/7800160 to your computer and use it in GitHub Desktop.
Fetches the IAM instance profile provided credentials inside of an EC2 instance and stores them in environment variables. usage: source iam-store-credentials.sh
#!/bin/bash
IAM_INFO=$(GET http://169.254.169.254/latest/meta-data/iam/info)
IAM_INSTANCE_PROFILE=$(echo "${IAM_INFO}" | sed -n 's_^[^/]*/\([^"]*\)".*_\1_p')
IAM_CREDENTIALS_URL="http://169.254.169.254/latest/meta-data/iam/security-credentials/$IAM_INSTANCE_PROFILE"
IAM_CREDENTIALS=$(GET $IAM_CREDENTIALS_URL)
export AWS_ACCESS_KEY=$(echo "${IAM_CREDENTIALS}" | sed -n 's_.*"AccessKeyId" : "\([^"]*\)".*_\1_p')
export AWS_SECRET_KEY=$(echo "${IAM_CREDENTIALS}" | sed -n 's_.*"SecretAccessKey" : "\([^"]*\)".*_\1_p')
export AWS_DELEGATION_TOKEN=$(echo "${IAM_CREDENTIALS}" | sed -n 's_.*"Token" : "\([^"]*\)".*_\1_p')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment