Skip to content

Instantly share code, notes, and snippets.

@rosenhouse
Created December 10, 2013 04:45
Show Gist options
  • Save rosenhouse/7885843 to your computer and use it in GitHub Desktop.
Save rosenhouse/7885843 to your computer and use it in GitHub Desktop.
Extract temporary AWS credentials that an EC2 instance has assumed.
import sys
import json
import urllib
def print_creds(iam_role_name):
'''
Print out the current AWS credentials that this EC2 instance
has been assigned, for a IAM role it has assumed.
Note that credentials are rotated frequently
'''
url_prefix="http://169.254.169.254/latest/meta-data/iam/security-credentials/"
data = urllib.urlopen(url_prefix + iam_role_name).read()
creds = json.loads(data)
print "AWS_ACCESS_KEY_ID=" + creds["AccessKeyId"]
print "AWS_SECRET_ACCESS_KEY=" + creds["SecretAccessKey"]
if __name__ == "__main__":
iam_role_name = "dev-support"
if len(sys.argv) > 1:
iam_role_name=sys.argv[1]
print_creds(iam_role_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment