[PYTHON] I didn't want to write the AWS key in the program

What I wrote

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

Run

$ python aws_credentials.py default
aws_access_key: xxxxxxxxxx
aws_secret_key: zzzzzzzzzzz

bonus

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

I didn't want to write the AWS key in the program
Various comments to write in the program
I want to display the progress in Python!
I want to write in Python! (1) Code format check
I want to write in Python! (2) Let's write a test
I want to use the R dataset in python
I didn't have to write a decorator in the class Thank you contextmanager
I want to align the significant figures in the Numpy array
[Linux] I want to know the date when the user logged in
I wrote the code to write the code of Brainf * ck in python
LINEbot development, I want to check the operation in the local environment
I want to create a system to prevent forgetting to tighten the key 1
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I want to make the second line the column name in pandas
I want to pass the G test in one month Day 1
I want to know the population of each country in the world.
I want to pin Spyder to the taskbar
I want to output to the console coolly
I want to print in a comprehension
I want to handle the rhyme part1
I want to handle the rhyme part3
I want to play with aws with python
I want to display the progress bar
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to change the color by clicking the scatter point in matplotlib
[C language] I want to generate random numbers in the specified range
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
I want to use the Django Debug Toolbar in my Ajax application
I want to leave an arbitrary command in the command history of Shell
I made a program to check the size of a file in Python
I want to do Dunnett's test in Python
I want to pin Datetime.now in Django tests
I want to create a window in Python
I want to handle the rhyme part7 (BOW)
The trick to write flatten concisely in python
I want to customize the appearance of zabbix
I want to write to a file with Python
I want to use the activation function Mish
[Python] I want to know the variables in the function when an error occurs!
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I want to use Python in the environment of pyenv + pipenv on Windows 10
[Django memo] I want to set the login user information in the form in advance
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I want to store the result of% time, %% time, etc. in an object (variable)
Python program is slow! I want to speed up! In such a case ...
I want to see the file name from DataLoader
I tried to graph the packages installed in Python
I want to easily implement a timeout in python
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to grep the execution result of strace
Even in JavaScript, I want to see Python `range ()`!
I want to randomly sample a file in Python
I want to inherit to the back with python dataclass