Skip to content

Commit c30ed7c

Browse files
author
Tom McCarthy
committed
docs: Update docs to reflect new support for composite keys in DynamoDB
1 parent 6c451f7 commit c30ed7c

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

docs/utilities/idempotency.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,18 @@ This persistence layer is built-in, and you can either use an existing DynamoDB
321321
)
322322
```
323323

324-
These are knobs you can use when using DynamoDB as a persistence layer:
324+
When using DynamoDB as a persistence layer, you can alter the attribute names by passing these parameters when initializing the persistence layer:
325325

326326
Parameter | Required | Default | Description
327327
------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------
328328
**table_name** | :heavy_check_mark: | | Table name to store state
329-
**key_attr** | | `id` | Primary key of the table. Hashed representation of the payload
329+
**key_attr** | | `id` | Partition key of the table. Hashed representation of the payload (unless **sort_key_attr** is specified)
330330
**expiry_attr** | | `expiration` | Unix timestamp of when record expires
331331
**status_attr** | | `status` | Stores status of the lambda execution during and after invocation
332332
**data_attr** | | `data` | Stores results of successfully executed Lambda handlers
333333
**validation_key_attr** | | `validation` | Hashed representation of the parts of the event used for validation
334+
**sort_key_attr** | | | Sort key of the table (if table is configured with a sort key).
335+
**static_pk_value** | | `idempotency#{LAMBDA_FUNCTION_NAME}` | Static value to use as the partition key. Only used when **sort_key_attr** is set.
334336

335337
## Advanced
336338

@@ -590,6 +592,36 @@ The **`boto_config`** and **`boto3_session`** parameters enable you to pass in a
590592
...
591593
```
592594

595+
### Using a DynamoDB table with a composite primary key
596+
597+
If you wish to use this utility with a DynamoDB table that is configured with a composite primary key (uses both partition key and sort key), you
598+
should set the `sort_key_attr` parameter when initializing your persistence layer. When this parameter is set, the partition key value for all idempotency entries
599+
will be the same, with the idempotency key being saved as the sort key instead of the partition key. You can optionally set a static value for the partition
600+
key using the `static_pk_value` parameter. If not specified, it will default to `idempotency#{LAMBDA_FUNCTION_NAME}`.
601+
602+
=== "MyLambdaFunction"
603+
604+
```python hl_lines="5"
605+
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, idempotent
606+
607+
persistence_layer = DynamoDBPersistenceLayer(
608+
table_name="IdempotencyTable",
609+
sort_key_attr='sort_key')
610+
611+
612+
@idempotent(persistence_store=persistence_layer)
613+
def handler(event, context):
614+
return {"message": "success": "id": event['body']['id]}
615+
```
616+
617+
The example function above would cause data to be stored in DynamoDB like this:
618+
619+
| id | sort_key | expiration | status | data |
620+
|------------------------------|----------------------------------|------------|-------------|-------------------------------------|
621+
| idempotency#MyLambdaFunction | 1e956ef7da78d0cb890be999aecc0c9e | 1636549553 | COMPLETED | {"id": 12391, "message": "success"} |
622+
| idempotency#MyLambdaFunction | 2b2cdb5f86361e97b4383087c1ffdf27 | 1636549571 | COMPLETED | {"id": 527212, "message": "success"}|
623+
| idempotency#MyLambdaFunction | f091d2527ad1c78f05d54cc3f363be80 | 1636549585 | IN_PROGRESS | |
624+
593625
### Bring your own persistent store
594626

595627
This utility provides an abstract base class (ABC), so that you can implement your choice of persistent storage layer.

0 commit comments

Comments
 (0)