Skip to content

Instantly share code, notes, and snippets.

@dhobbs
Created July 2, 2014 14:51
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 dhobbs/d85a3fb2c6a223537feb to your computer and use it in GitHub Desktop.
Save dhobbs/d85a3fb2c6a223537feb to your computer and use it in GitHub Desktop.
Export IAM credentials as environment variables
#! /bin/bash
# download jq from http://stedolan.github.io/jq/
IAM_CREDS=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/[iam-role] 2>/dev/null`
export AWS_ACCESS_KEY_ID=`echo $IAM_CREDS | jq .AccessKeyId | sed -e 's/^"//' -e 's/"$//'`
export AWS_SECRET_ACCESS_KEY=`echo $IAM_CREDS | jq .SecretAccessKey | sed -e 's/^"//' -e 's/"$//'`
@dhobbs
Copy link
Author

dhobbs commented Jul 2, 2014

Source this file from an EC2 instance with an IAM role (replace [iam-role] with the name of your role) to get the credentials as environment variables, for tools that don't understand IAM.

(IAM credentials are short-lived, so you'll want to run this immediately before the thing that needs the variables).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment