You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/idempotency.md
+62-6
Original file line number
Diff line number
Diff line change
@@ -289,16 +289,40 @@ The client was successful in receiving the result after the retry. Since the Lam
289
289
290
290
### Handling exceptions
291
291
292
-
**The record in the persistence layer will be deleted** if your Lambda handler returns an exception. This means that new invocations will execute again despite having the same payload.
292
+
If you are using the `idempotent` decorator on your Lambda handler, any unhandled exceptions that are raised during the code execution will cause **the record in the persistence layer to be deleted**.
293
+
This means that new invocations will execute your code again despite having the same payload. If you don't want the record to be deleted, you need to catch exceptions within the idempotent function and return a successful response.
293
294
294
-
If you don't want the record to be deleted, you need to catch exceptions within the handler and return a successful response.
If you are using `idempotent_function`, any unhandled exceptions that are raised _inside_ the decorated function will cause the record in the persistence layer to be deleted, and allow the function to be executed again if retried.
299
+
If an Exception is raised _outside_ the scope of the decorated function and after your function has been called, the persistent record will not be affected. In this case, idempotency will be maintained for your decorated function. Example:
300
+
301
+
=== "app.py"
302
+
303
+
```python hl_lines="2-4 8-10"
304
+
deflambda_handler(event, context):
305
+
# If an exception is raised here, no idempotent record will ever get created as the
306
+
# idempotent function does not get called
307
+
do_some_stuff()
308
+
309
+
result = call_external_service(data={"user": "user1", "id": 5})
310
+
311
+
# This exception will not cause the idempotent record to be deleted, since it
312
+
# happens after the decorated function has been successfully called
result = requests.post('http://example.com', json={"user": data['user'], "transaction_id": data['id']}
319
+
return result.json()
320
+
```
321
+
298
322
!!! warning
299
-
**We will raise `IdempotencyPersistenceLayerError`** if any of the calls to the persistence layer fail unexpectedly.
323
+
**We will raise`IdempotencyPersistenceLayerError`**ifany of the calls to the persistence layer fail unexpectedly.
300
324
301
-
As this happens outside the scope of your Lambda handler, you are not going to be able to catch it.
325
+
As this happens outside the scope of your decorated function, you are not able to catch itif you're using the `idempotent` decorator on your Lambda handler.
302
326
303
327
### Persistence layers
304
328
@@ -321,16 +345,18 @@ This persistence layer is built-in, and you can either use an existing DynamoDB
321
345
)
322
346
```
323
347
324
-
These are knobs you can use when using DynamoDB as a persistence layer:
348
+
When using DynamoDB as a persistence layer, you can alter the attribute names by passing these parameters when initializing the persistence layer:
**validation_key_attr** | | `validation` | Hashed representation of the parts of the event used forvalidation
358
+
**sort_key_attr** | | | Sort key of the table (if table is configured with a sort key).
359
+
**static_pk_value**||`idempotency#{LAMBDA_FUNCTION_NAME}` | Static value to use as the partition key. Only used when **sort_key_attr** is set.
334
360
335
361
## Advanced
336
362
@@ -590,6 +616,36 @@ The **`boto_config`** and **`boto3_session`** parameters enable you to pass in a
590
616
...
591
617
```
592
618
619
+
### Using a DynamoDB table with a composite primary key
620
+
621
+
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
622
+
should set the `sort_key_attr` parameter when initializing your persistence layer. When this parameter isset, the partition key value forall idempotency entries
623
+
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
624
+
key using the `static_pk_value` parameter. If not specified, it will default to `idempotency#{LAMBDA_FUNCTION_NAME}`.
625
+
626
+
==="MyLambdaFunction"
627
+
628
+
```python hl_lines="5"
629
+
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, idempotent
0 commit comments