Skip to content

DynamoDB

Amazon DynamoDB parameter retrieval and caching utility

CLASS DESCRIPTION
DynamoDBProvider

Amazon DynamoDB Parameter Provider

DynamoDBProvider

DynamoDBProvider(
    table_name: str,
    key_attr: str = "id",
    sort_attr: str = "sk",
    value_attr: str = "value",
    endpoint_url: str | None = None,
    config: Config | None = None,
    boto_config: Config | None = None,
    boto3_session: boto3.session.Session | None = None,
    boto3_client: DynamoDBServiceResource | None = None,
)

Bases: BaseProvider

Amazon DynamoDB Parameter Provider

PARAMETER DESCRIPTION
table_name

Name of the DynamoDB table that stores parameters

TYPE: str

key_attr

Hash key for the DynamoDB table (default to 'id')

TYPE: str DEFAULT: 'id'

sort_attr

Name of the DynamoDB table sort key (defaults to 'sk'), used only for get_multiple

TYPE: str DEFAULT: 'sk'

value_attr

Attribute that contains the values in the DynamoDB table (defaults to 'value')

TYPE: str DEFAULT: 'value'

endpoint_url

Complete url to reference local DynamoDB instance, e.g. http://localhost:8080

TYPE: str | None DEFAULT: None

config

Botocore configuration to pass during client initialization

TYPE: Config | None DEFAULT: None

boto3_session
1
Boto3 session to create a boto3_client from

TYPE: Session DEFAULT: None

boto3_client
1
Boto3 DynamoDB Resource Client to use; boto3_session will be ignored if both are provided

TYPE: DynamoDBServiceResource | None DEFAULT: None

Example

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. The parameter item looks like this:

1
2
3
4
5
6
7
8
9
{ "id": "my-parameters", "value": "Parameter value a" }

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider(
...     "ParametersTable",
...     key_attr="my-id",
...     value_attr="my-value"
... )
>>>
>>> value = ddb_provider.get("my-parameter")
>>>
>>> print(value)
My parameter value

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> 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)
>>>
>>> value = ddb_provider.get("my-parameter")
>>>
>>> print(value)
My parameter value

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

1
2
3
4
5
6
7
>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> value = ddb_provider.get("my-parameter", ConsistentRead=True)
>>>
>>> print(value)
My parameter value

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. The DynamoDB table contains three items looking like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{ "id": "my-parameters", "sk": "a", "value": "Parameter value a" }
{ "id": "my-parameters", "sk": "b", "value": "Parameter value b" }
{ "id": "my-parameters", "sk": "c", "value": "Parameter value c" }

>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> values = ddb_provider.get_multiple("my-parameters")
>>>
>>> for key, value in values.items():
...     print(key, value)
a   Parameter value a
b   Parameter value b
c   Parameter value c

Retrieves multiple values from a DynamoDB table that has custom attribute names

In this case, the provider will use a sort key to retrieve multiple values using a query under the hood.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider(
...     "ParametersTable",
...     key_attr="my-id",
...     sort_attr="my-sort-key",
...     value_attr="my-value"
... )
>>>
>>> values = ddb_provider.get_multiple("my-parameters")
>>>
>>> for key, value in values.items():
...     print(key, value)
a   Parameter value a
b   Parameter value b
c   Parameter value c

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> from aws_lambda_powertools.utilities.parameters import DynamoDBProvider
>>> ddb_provider = DynamoDBProvider("ParametersTable")
>>>
>>> values = ddb_provider.get_multiple("my-parameters", ConsistentRead=True)
>>>
>>> for key, value in values.items():
...     print(key, value)
a   Parameter value a
b   Parameter value b
c   Parameter value c
Source code in aws_lambda_powertools/utilities/parameters/dynamodb.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def __init__(
    self,
    table_name: str,
    key_attr: str = "id",
    sort_attr: str = "sk",
    value_attr: str = "value",
    endpoint_url: str | None = None,
    config: Config | None = None,
    boto_config: Config | None = None,
    boto3_session: boto3.session.Session | None = None,
    boto3_client: DynamoDBServiceResource | None = None,
):
    """
    Initialize the DynamoDB client
    """
    if config:
        warnings.warn(
            message="The 'config' parameter is deprecated in V3 and will be removed in V4. "
            "Please use 'boto_config' instead.",
            category=PowertoolsDeprecationWarning,
            stacklevel=2,
        )

    if boto3_client is None:
        boto3_session = boto3_session or boto3.session.Session()
        boto3_client = boto3_session.resource("dynamodb", config=boto_config or config, endpoint_url=endpoint_url)

    self.table = boto3_client.Table(table_name)
    self.key_attr = key_attr
    self.sort_attr = sort_attr
    self.value_attr = value_attr

    super().__init__(resource=boto3_client)