So I decided to read from ~ / .aws / credentials
aws_credentials.py
import sys
import os
aws_profile = sys.argv[1]
credentials_path = os.path.expanduser("~/.aws/credentials")
with open(credentials_path, "r") as f:
line = f.readline()
while line:
if "[{}]".format(aws_profile) in line:
line_access_key = f.readline()
aws_access_key = line_access_key.split("=")[1].strip()
line_secret_key = f.readline()
aws_secret_key = line_secret_key.split("=")[1].strip()
break
line = f.readline()
print "aws_access_key: %s" % aws_access_key
print "aws_secret_key: %s" % aws_secret_key
$ python aws_credentials.py default
aws_access_key: xxxxxxxxxx
aws_secret_key: zzzzzzzzzzz
Suppose the contents of ~ / .aws / credentials look like this
~/.aws/credentials
[default]
aws_access_key_id = xxxxxxxxxx
aws_secret_access_key = zzzzzzzzzzz
[fuck]
aws_access_key_id = shit
aws_secret_access_key = bitch
Recommended Posts