DynamoDBConfig
This configuration loads the configuration data from a DynamoDB table, all data types from DynamoDB table are supported.
Library boto3 is required (pip install boto3)
The examples below use AWS CLI to create tables and insert items into them, you can find here further info about AWS CLI.
aws dynamodb create-table \
--table-name config \
--attribute-definitions AttributeName=key,AttributeType=S \
--key-schema AttributeName=key,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
aws dynamodb put-item \
--table-name config \
--item '{"key": {"S": "database.host"}, "value": {"S": "localhost"}}'
aws dynamodb put-item \
--table-name config \
--item '{"key": {"S": "database.port"}, "value": {"N": "1234"}}'
# main.py
import boto3
from central.config.dynamodb import DynamoDBConfig
dynamodb = boto3.resource('dynamodb')
config = DynamoDBConfig(dynamodb, 'config')
config.load()
print(config.get('database.host'))
print(config.get('database.port'))
python main.py
>> localhost
>> 1234
Using hash key and sort key
DynamoDBConfig
also supports hash and range keys, the hash key is used as a filter and the range key as a configuration key, for example, you can use the hash key as the application name so that only configuration keys for this specific application will be loaded.
aws dynamodb create-table \
--table-name config \
--attribute-definitions AttributeName=context,AttributeType=S AttributeName=key,AttributeType=S \
--key-schema AttributeName=context,KeyType=HASH AttributeName=key,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
aws dynamodb put-item \
--table-name config \
--item '{"context": {"S": "myapp"}, "key": {"S": "database.host"}, "value": {"S": "localhost"}}'
aws dynamodb put-item \
--table-name config \
--item '{"context": {"S": "myapp"}, "key": {"S": "database.port"}, "value": {"N": "1234"}}'
# main.py
import boto3
from central.config.dynamodb import DynamoDBConfig
dynamodb = boto3.resource('dynamodb')
config = DynamoDBConfig(dynamodb, 'config', context_value='appname')
config.load()
print(config.get('database.host'))
print(config.get('database.port'))
python main.py
>> localhost
>> 1234
Choosing attribute names
DynamoDBConfig
by default uses 'context'
as the hash key attribute name if a context value is set, 'key'
as the range key attribute name if context value is set otherwise, as the hash key attribute name and 'value'
as value attribute name.
You can define by yourself the attribute names.
import boto3
from central.config.dynamodb import DynamoDBConfig
dynamodb = boto3.resource('dynamodb')
config = DynamoDBConfig(dynamodb, 'config', context_attribute='property_context',
key_attribute='property_key', value_attribute='property_value')
Reloading configuration
Amazon DynamoDB does not support update notification so if your application needs to be notified when a configuration has been changed it can be archived using polling.
import boto3
from central.config.dynamodb import DynamoDBConfig
dynamodb = boto3.resource('dynamodb')
config = DynamoDBConfig(dynamodb, 'config').reload_every(600) # 10 min in seconds
config.load()