Use parameter store in Python

Introduction

I remembered that I had to use the Parameter Store (AWS Systems Manager) that I used at work again.

This time as well, Poetry + Docker is used as the development environment. The source is listed on Github.

Set Parameter Store

Created to start with / aaa スクリーンショット 2020-10-19 16.58.17.png

File

The files are separated as main and library.

main.py



from src.ssm_manager import SsmManager

print("start")

ssm_manager = SsmManager(region_name="ap-northeast-1")
ssm_manager.load_parameter(base_ssm_path="/aaa")
print(ssm_manager.parameters)

ssm_manager.py


from typing import List, Dict

import boto3


class SsmManager:
    def __init__(self, region_name: str):
        self.__ssm = boto3.client('ssm', region_name=region_name)
        self.__parameters = []
        self.__base_ssm_path = None

    @property
    def parameters(self) -> List[Dict[str, any]]:
        return [{
            'name': item['Name'].replace(f'{self.__base_ssm_path}/', ''),
            'value': item['Value']
        } for item in self.__parameters]

    def load_parameter(self, base_ssm_path: str) -> None:
        self.__base_ssm_path = base_ssm_path
        result = []
        next_token = None
        while True:
            dict_parameter = {
                'Path': base_ssm_path,
                'Recursive': True,
                'WithDecryption': True,
            }
            if next_token is not None:
                dict_parameter['NextToken'] = next_token
            response = self.__ssm.get_parameters_by_path(**dict_parameter)
            parameters = response['Parameters']
            result.extend(parameters)
            if 'NextToken' not in response:
                break
            next_token = response['NextToken']
        self.__parameters = result

At the end

Now you can save the RDS password etc. that you put in the environment variables etc.

Recommended Posts

Use parameter store in Python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
Let's use def in python
Use let expression in Python
Use Measurement Protocol in Python
Use callback function in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Transfer parameter values in Python
Use fabric as is in python (fabric3)
FX Systre Parameter Optimization in Python
HMM parameter estimation implementation in python
How to use SQLite in Python
Use rospy with virtualenv in Python3
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Use OpenCV with Python 3 in Window
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
[Introduction to Python] How to use class in Python?
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Easily use your own functions in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Easy way to use Wikipedia in Python
Plink in Python
Constant in python
Don't use \ d in Python 3 regular expressions!
Lifegame in Python.
FizzBuzz in Python
How to use __slots__ in Python class
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7