2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

俺はAWSのキーをプログラム中に書きたくなかった

Posted at

書いたもの

なので~/.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
2
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?