Load parameters from AWS Parameter Store
Project description
AWStanding
Easily load variables from AWS Parameter store into environment variables.
Why to use AWStanding?
Despite it's built on top of Boto3, it has the following key features that eases the development process:
- Simpler API
- Error Handling
- Pagination handling when needed (Saves you a buch of boilerplate)
- Dynamic Parameters (variables that listen to updates on AWS)
- S3 Integration made easy with Download/Upload methods
Installation
pip install awstanding
I personally recommend using pipenv:
pipenv install awstanding
Quickstart
from awstanding.parameter_store import load_parameters
load_parameters({'/some/path/to/something/stored': 'IMPORTANT_SETTING'})
import os
print(os.environ.get('IMPORTANT_SETTING'))
'super-important-value'
Using with python-decouple
import os
from awstanding.parameter_store import load_parameters
from decouple import config
load_parameters({'/some/path/to/something/stored': 'IMPORTANT_SETTING'})
IMPORTANT_SETTING = config('IMPORTANT_SETTING', default='some-default')
print(IMPORTANT_SETTING)
'super-important-value'
Not allowing missing parameters
from awstanding.parameter_store import load_parameters
# A call like this one:
load_parameters({'/not/defined/parameter': 'IMPORTANT_SETTING'}, allow_invalid=False)
# will raise a ParameterNotFoundException, and you can handle it as follows:
from awstanding.exceptions import ParameterNotFoundException
try:
load_parameters({'/not/defined/parameter': 'IMPORTANT_SETTING'}, allow_invalid=False)
except ParameterNotFoundException as e:
# perform any cleanup action..
Performance
Amount of parameters | Missing parameters | AWStanding | SSM client calls |
---|---|---|---|
40 | 0 | ~3.1s | ~15.5s |
40 | 0 | ~2.4s | ~15.3s |
40 | 0 | ~4.6s | ~14.5s |
40 | 0 | ~2.5s | ~15.5s |
40 | 1 | ~2.1s | error: ParameterNotFound |
40 | 20 | ~2.2s | error: ParameterNotFound |
40 | 40 | ~2.1s | error: ParameterNotFound |
80 | 40 | ~3.5s | error: ParameterNotFound |
80 | 40 | ~3.9s | (using try..except) ~32.7s |
Loading paths
Suppose you have defined these variables in ParameterStore:
'/stripe/price/'
'/stripe/webhook/' # (Let's not define this one just for demonstration)
You can leverage on the good naming and perform a path variable loading as follows:
import os
from awstanding.parameter_store import load_path
load_path('/stripe', '/spotify')
STRIPE_PRICE = os.environ.get('STRIPE_PRICE', 'fallback_value')
STRIPE_WEBHOOK = os.environ.get('STRIPE_WEBHOOK', 'fallback_value')
SPOTIFY_API_KEY = os.environ.get('SPOTIFY_API_KEY', 'fallback_value')
print(f'price: {STRIPE_PRICE}, webhook: {STRIPE_WEBHOOK}, spotify: {SPOTIFY_API_KEY}')
>>> price: price_1xxxxxxxxxxxxxxxxxxxxxxx, webhook: fallback_value, spotify: fallback_value
Dynamic Parameters
You can define dynamic parameters that uploads themselves each time they are used, so you can update any parameter without re-deploy your service.
from awstanding.parameter_store import DynamicParameter
IMPORTANT_SETTING = DynamicParameter('/test/parameter')
print(IMPORTANT_SETTING)
>>> OriginalValue
# Someone updates /test/parameter on AWS...
print(IMPORTANT_SETTING)
>>> NewValue
Supported operations
Some useful operations are supported by the class itself, emulating built-in str class:
from awstanding.parameter_store import DynamicParameter
IMPORTANT_SETTING = DynamicParameter('/test/parameter')
# Equality comparison
equal = IMPORTANT_SETTING == 'SomeString'
# Length
length = len(IMPORTANT_SETTING)
# Concatenation (Right and Left)
concat = '~' + IMPORTANT_SETTING + '~'
# You can always convert the parameter to string to get full string capabilities:
str_IMPORTANT_SETTING = str(IMPORTANT_SETTING) # Have in mind this will "freeze" the value, so don't overwrite IMPORTANT_SETTING
S3 Integration
Download files from S3
from awstanding.s3 import Bucket
bucket = Bucket('BUCKET_NAME_HERE')
bucket.download("path/to/file.ext", './some/local/file.ext')
Upload files to S3
from awstanding.s3 import Bucket
bucket = Bucket('BUCKET_NAME_HERE')
bucket.upload('/some/local/file.ext', "some/s3/logical/path.ext")
There's not file type restriction any other that the set by AWS/boto3 itself.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file awstanding-1.2.0.tar.gz
.
File metadata
- Download URL: awstanding-1.2.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5664173cde78908ef5faa28f2d34ea6c5a5475b0518bdf35144c39cbf3e867a4 |
|
MD5 | 06223dd41a377687f451ee26f1b669ad |
|
BLAKE2b-256 | 4433648b91428d4e6bf6a9776b9957ce2d6117d03f56c8cfd0e036bdfb47afc1 |
File details
Details for the file awstanding-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: awstanding-1.2.0-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dc1581f6e01169921d4666ec9e880c5f5e7155d236b606ca26bd76b02f9bda6 |
|
MD5 | e0350bec7c913bcc2bc309beafd080f1 |
|
BLAKE2b-256 | fd900b27f613c145feb088420ca12bbbd7bdcb968a5189be36afc75b613fc3ea |