Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parameter utility #96

Merged
merged 31 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b6787f9
feat: add get_parameter utility
nmoutschen Jul 29, 2020
29d27b8
fix: add AWS_DEFAULT_REGION for boto3 tests
nmoutschen Jul 29, 2020
a805e74
revert "fix: add AWS_DEFAULT_REGION for boto3 tests"
nmoutschen Jul 29, 2020
37964d1
fix: fix AWS_DEFAULT_REGION for get_parameter tests
nmoutschen Jul 29, 2020
dc3a5ab
fix: fix AWS_DEFAULT_REGION for get_parameter tests
nmoutschen Jul 29, 2020
9731e0a
Merge branch 'parameter-utility' of github.com:nmoutschen/aws-lambda-…
nmoutschen Jul 30, 2020
79bff7e
chore: rename _get_from_external_store to _get
nmoutschen Jul 30, 2020
a120528
feat: add get_multiple for parameter providers
nmoutschen Jul 30, 2020
d1c57ef
tests: increase test coverage
nmoutschen Jul 30, 2020
285ac95
tests: increase test coverage (2)
nmoutschen Jul 30, 2020
4c76276
tests: increase coverage to 100%
nmoutschen Jul 30, 2020
6eb012f
fix: add get_parameters in __all__
nmoutschen Jul 30, 2020
e6416c0
chore: split parameter utilities into smaller files
nmoutschen Aug 17, 2020
de842a8
feat: use botocore.config.Config for parameter providers
nmoutschen Aug 17, 2020
3569e21
feat: make arguments explicits in parameter utilities
nmoutschen Aug 17, 2020
1278d2c
docs: add examples for parameter utilities
nmoutschen Aug 17, 2020
5fb929d
feat: add override SDK options for parameter utilities
nmoutschen Aug 17, 2020
7438766
docs: add examples for shorthands in the parameter utility
nmoutschen Aug 17, 2020
d53c373
fix: fix typo in DynamoDB parameter example
nmoutschen Aug 18, 2020
fce3268
feat: throw exception on failed transform for parameter utility
nmoutschen Aug 18, 2020
c765c90
docs: add examples on how to retrieve parameters in the parameter uti…
nmoutschen Aug 18, 2020
bec8de3
feat: use paginator for SSM parameter utility
nmoutschen Aug 18, 2020
3ddc3bd
feat: make SSM parameter provider recursive by default
nmoutschen Aug 18, 2020
5502215
feat: move sort_attr to init for DynamoDB parameter provider
nmoutschen Aug 18, 2020
c8c970f
feat: add 'raise_on_transform_error' for get_multiple parameter utility
nmoutschen Aug 19, 2020
ed45c4b
docs: add sdk_options to parameters for get and get_multiple
nmoutschen Aug 19, 2020
4ecb17b
docs: add documentation for parameters utility
nmoutschen Aug 19, 2020
616a98d
docs: add passing arguments to SDK
nmoutschen Aug 19, 2020
68f6beb
docs: restructure based on feedback
nmoutschen Aug 21, 2020
dd3053d
docs: tweaks based on feedback
nmoutschen Aug 21, 2020
7b87dfa
improv: iam permissions table
heitorlessa Aug 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws_lambda_powertools/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

"""General utilities for Powertools"""
22 changes: 22 additions & 0 deletions aws_lambda_powertools/utilities/parameters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved

"""
Parameter retrieval and caching utility
"""

from .base import BaseProvider
heitorlessa marked this conversation as resolved.
Show resolved Hide resolved
from .dynamodb import DynamoDBProvider
from .exceptions import GetParameterError
from .secrets import SecretsProvider, get_secret
from .ssm import SSMProvider, get_parameter, get_parameters

__all__ = [
"BaseProvider",
"GetParameterError",
"DynamoDBProvider",
"SecretsProvider",
"SSMProvider",
"get_parameter",
"get_parameters",
"get_secret",
]
124 changes: 124 additions & 0 deletions aws_lambda_powertools/utilities/parameters/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"""
Base for Parameter providers
"""

import base64
import json
from abc import ABC, abstractmethod
from collections import namedtuple
from datetime import datetime, timedelta
from typing import Dict, Optional, Union

from .exceptions import GetParameterError

DEFAULT_MAX_AGE_SECS = 5
ExpirableValue = namedtuple("ExpirableValue", ["value", "ttl"])
# These providers will be dynamically initialized on first use of the helper functions
DEFAULT_PROVIDERS = {}


class BaseProvider(ABC):
"""
Abstract Base Class for Parameter providers
"""

store = None

def __init__(self):
"""
Initialize the base provider
"""

self.store = {}

def get(
self, name: str, max_age: int = DEFAULT_MAX_AGE_SECS, transform: Optional[str] = None, **kwargs
) -> Union[str, list, dict, bytes]:
"""
Retrieve a parameter value or return the cached value

Parameters
----------

name: str
Parameter name
max_age: int
Maximum age of the cached value
transform: str
Optional transformation of the parameter value. Supported values
are "json" for JSON strings and "binary" for base 64 encoded
values.

Raises
------

GetParameterError
When the parameter provider fails to retrieve a parameter value for
a given name.
"""

# If there are multiple calls to the same parameter but in a different
# transform, they will be stored multiple times. This allows us to
# optimize by transforming the data only once per retrieval, thus there
# is no need to transform cached values multiple times. However, this
# means that we need to make multiple calls to the underlying parameter
# store if we need to return it in different transforms. Since the number
# of supported transform is small and the probability that a given
# parameter will always be used in a specific transform, this should be
# an acceptable tradeoff.
key = (name, transform)

if key not in self.store or self.store[key].ttl < datetime.now():
try:
value = self._get(name, **kwargs)
# Encapsulate all errors into a generic GetParameterError
except Exception as exc:
raise GetParameterError(str(exc))

if transform == "json":
value = json.loads(value)
elif transform == "binary":
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved
value = base64.b64decode(value)

self.store[key] = ExpirableValue(value, datetime.now() + timedelta(seconds=max_age),)

return self.store[key].value

@abstractmethod
def _get(self, name: str, **kwargs) -> str:
"""
Retrieve paramater value from the underlying parameter store
"""
raise NotImplementedError()

def get_multiple(
self, path: str, max_age: int = DEFAULT_MAX_AGE_SECS, transform: Optional[str] = None, **kwargs
) -> Union[Dict[str, str], Dict[str, dict], Dict[str, bytes]]:
"""
Retrieve multiple parameters based on a path prefix
"""

key = (path, transform)

if key not in self.store or self.store[key].ttl < datetime.now():
try:
values = self._get_multiple(path, **kwargs)
# Encapsulate all errors into a generic GetParameterError
except Exception as exc:
raise GetParameterError(str(exc))

if transform == "json":
values = {k: json.loads(v) for k, v in values.items()}
elif transform == "binary":
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved
values = {k: base64.b64decode(v) for k, v in values.items()}

self.store[key] = ExpirableValue(values, datetime.now() + timedelta(seconds=max_age),)

return self.store[key].value

@abstractmethod
def _get_multiple(self, path: str, **kwargs) -> Dict[str, str]:
"""
Retrieve multiple parameter values from the underlying parameter store
"""
raise NotImplementedError()
164 changes: 164 additions & 0 deletions aws_lambda_powertools/utilities/parameters/dynamodb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
"""
Amazon DynamoDB parameter retrieval and caching utility
"""


from typing import Dict, Optional

import boto3
from boto3.dynamodb.conditions import Key
from botocore.config import Config

from .base import BaseProvider


class DynamoDBProvider(BaseProvider):
"""
Amazon DynamoDB Parameter Provider

Parameters
----------
table_name: str
Name of the DynamoDB table that stores parameters
key_attr: str, optional
Hash key for the DynamoDB table
value_attr: str, optional
Attribute that contains the values in the DynamoDB table
config: botocore.config.Config, optional
Botocore configuration to pass during client initialization

Example
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved
-------
**Retrieves a parameter value from a DynamoDB table**

In this example, the DynamoDB table uses `id` as hash key and stores the value in the `value`
attribute.

>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> ddb_provider.get("my-parameter")

**Retrieves a parameter value from a DynamoDB table that has custom attribute names**

>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider(
... "ParametersTable",
... key_attr="my-id",
... value_attr="my-value"
... )
>>>
>>> ddb_provider.get("my-parameter")

**Retrieves a parameter value from a DynamoDB table in another AWS region**

>>> from botocore.config import Config
>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>>
>>> config = Config(region_name="us-west-1")
>>> ddb_provider = DynamoDBProvider("ParametersTable", config=config)
>>>
>>> ddb_provider.get("my-parameter")

**Retrieves a parameter value from a DynamoDB table passing options to the SDK call**

>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> ddb_provider.get("my-parameter", ConsistentRead=True)

**Retrieves multiple values from a DynamoDB table**

In this case, the provider will use a sort key to retrieve multiple values using a query under
the hood. This expects that the sort key is named `sk`.

>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> ddb_provider.get_multiple("my-parameters")

**Retrieves multiple values from a DynamoDB table with a custom sort key**

In this case, the provider will use a sort key to retrieve multiple values using a query under
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved
the hood.

>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> ddb_provider.get_multiple("my-parameters", sort_attr="my-sort-attr")

**Retrieves multiple values from a DynamoDB table passing options to the SDK calls**

>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> ddb_provider.get("my-parameter", ConsistentRead=True)
"""

table = None
key_attr = None
value_attr = None

def __init__(
self, table_name: str, key_attr: str = "id", value_attr: str = "value", config: Optional[Config] = None,
):
"""
Initialize the DynamoDB client
"""

config = config or Config()
self.table = boto3.resource("dynamodb", config=config).Table(table_name)

self.key_attr = key_attr
self.value_attr = value_attr

super().__init__()

def _get(self, name: str, **sdk_options) -> str:
"""
Retrieve a parameter value from Amazon DynamoDB

Parameters
----------
name: str
Name of the parameter
sdk_options: dict, optional
Dictionary of options that will be passed to the get_item call
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved
"""

# Explicit arguments will take precedence over keyword arguments
sdk_options["Key"] = {self.key_attr: name}
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved

return self.table.get_item(**sdk_options)["Item"][self.value_attr]

def _get_multiple(self, path: str, sort_attr: str = "sk", **sdk_options) -> Dict[str, str]:
"""
Retrieve multiple parameter values from Amazon DynamoDB

Parameters
----------
path: str
Path to retrieve the parameters
sort_attr: str, optional
Name of the DynamoDB table sort key (defaults to 'sk')
sdk_options: dict, optional
Dictionary of options that will be passed to the query call
nmoutschen marked this conversation as resolved.
Show resolved Hide resolved
"""

# Explicit arguments will take precedence over keyword arguments
sdk_options["KeyConditionExpression"] = Key(self.key_attr).eq(path)

response = self.table.query(**sdk_options)
items = response.get("Items", [])

# Keep querying while there are more items matching the partition key
while "LastEvaluatedKey" in response:
sdk_options["ExclusiveStartKey"] = response["LastEvaluatedKey"]
response = self.table.query(**sdk_options)
items.extend(response.get("Items", []))

retval = {}
for item in items:
retval[item[sort_attr]] = item[self.value_attr]

return retval
7 changes: 7 additions & 0 deletions aws_lambda_powertools/utilities/parameters/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Parameter retrieval exceptions
"""


class GetParameterError(Exception):
"""When a provider raises an exception on parameter retrieval"""
Loading