書いたもの
なので~/.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
おまけ
~/.aws/credentialsの中身はこんな感じだとする
~/.aws/credentials
[default]
aws_access_key_id = xxxxxxxxxx
aws_secret_access_key = zzzzzzzzzzz
[fuck]
aws_access_key_id = shit
aws_secret_access_key = bitch