Created
December 10, 2013 04:45
-
-
Save rosenhouse/7885843 to your computer and use it in GitHub Desktop.
Extract temporary AWS credentials that an EC2 instance has assumed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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