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
+85-46
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ The idempotency utility provides a simple solution to convert your Lambda functi
14
14
* Select a subset of the event as the idempotency key using JMESPath expressions
15
15
* Set a time window in which records with the same payload should be considered duplicates
16
16
* Expires in-progress executions if the Lambda function times out halfway through
17
+
* Support Amazon DynamoDB and Redis as persistence layer
17
18
18
19
## Terminology
19
20
@@ -65,7 +66,7 @@ Your Lambda function IAM Role must have `dynamodb:GetItem`, `dynamodb:PutItem`,
65
66
66
67
Before getting started, you need to create a persistent storage layer where the idempotency utility can store its state - your lambda functions will need read and write access to it.
67
68
68
-
As of now, Amazon DynamoDB is the only supported persistent storage layer, so you'll need to create a table first.
69
+
We currently support Amazon DynamoDB and Redis as a storage layer. This example demonstrates how to create a table in DynamoDB. If you want to use Redis, please go to the section [RedisPersistenceLayer](#redispersistencelayer)
69
70
70
71
**Default table configuration**
71
72
@@ -336,6 +337,51 @@ If an Exception is raised _outside_ the scope of the decorated function and afte
336
337
337
338
As this happens outside the scope of your decorated function, you are not able to catch it if you're using the `idempotent` decorator on your Lambda handler.
338
339
340
+
### Persistence layers
341
+
342
+
#### DynamoDBPersistenceLayer
343
+
344
+
This persistence layer is built-in, and you can either use an existing DynamoDB table or create a new one dedicated for idempotency state (recommended).
345
+
346
+
=== "Customizing DynamoDBPersistenceLayer to suit your table structure"
|**table_name**|:heavy_check_mark:|| Table name to store state |
357
+
|**key_attr**||`id`| Partition key of the table. Hashed representation of the payload (unless **sort_key_attr** is specified) |
358
+
|**expiry_attr**||`expiration`| Unix timestamp of when record expires |
359
+
|**in_progress_expiry_attr**||`in_progress_expiration`| Unix timestamp of when record expires while in progress (in case of the invocation times out) |
360
+
|**status_attr**||`status`| Stores status of the lambda execution during and after invocation |
361
+
|**data_attr**||`data`| Stores results of successfully executed Lambda handlers |
362
+
|**validation_key_attr**||`validation`| Hashed representation of the parts of the event used for validation |
363
+
|**sort_key_attr**||| Sort key of the table (if table is configured with a sort key). |
364
+
|**static_pk_value**||`idempotency#{LAMBDA_FUNCTION_NAME}`| Static value to use as the partition key. Only used when **sort_key_attr** is set. |
365
+
366
+
#### RedisPersistenceLayer
367
+
368
+
This persistence layer is built-in, and you can use an existing Redis service. For optimal performance and compatibility, we strongly advise using a Redis service version 7 or higher.
369
+
370
+
=== "Customizing RedisPersistenceLayer to suit your data structure"
|**in_progress_expiry_attr**||`in_progress_expiration`| Unix timestamp of when record expires while in progress (in case of the invocation times out) |
381
+
|**status_attr**||`status`| Stores status of the lambda execution during and after invocation |
382
+
|**data_attr**||`data`| Stores results of successfully executed Lambda handlers |
383
+
|**validation_key_attr**||`validation`| Hashed representation of the parts of the event used for validation |
384
+
339
385
### Idempotency request flow
340
386
341
387
The following sequence diagrams explain how the Idempotency feature behaves under different scenarios.
@@ -538,15 +584,23 @@ You need an existing Redis service before setting up Redis as persistent storage
538
584
???+ tip "No existing Redis service?"
539
585
If you don't have an existing Redis service, we recommend using [DynamoDB](#dynamodbpersistencelayer) as persistent storage layer provider.
1. Replace the Security Group ID and Subnet ID to match your VPC settings.
594
+
541
595
### VPC Access
542
596
543
-
Your Lambda Function must be able to reach the Redis endpoint before using it for idempotency persistent storage layer. In most cases you will need to [configure VPC access](https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html) for your Lambda Fucntion. Using a public accessable Redis is not recommended.
597
+
Your Lambda Function must be able to reach the Redis endpoint before using it for idempotency persistent storage layer. In most cases you will need to [configure VPC access](https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html) for your Lambda Function. Using a public accessible Redis is not recommended.
544
598
545
599
???+ tip "Amazon ElastiCache/MemoryDB for Redis as persistent storage layer provider"
546
600
If you intend to use Amazon ElastiCache for Redis for idempotency persistent storage layer, you can also reference [This AWS Tutorial](https://docs.aws.amazon.com/lambda/latest/dg/services-elasticache-tutorial.html).
547
601
If you are using Amazon MemoryDB for Redis, reference [This AWS Tutorial](https://aws.amazon.com/blogs/database/access-amazon-memorydb-for-redis-from-aws-lambda/) for only VPC setup part.
548
602
549
-
After VPC setup, you can follow the templates down below to setup Lambda fucntions with VPC internal subnet access.
603
+
After VPC setup, you can follow the templates down below to setup Lambda functions with VPC internal subnet access.
550
604
551
605
=== "AWS Serverless Application Model (SAM) example"
552
606
@@ -556,12 +610,12 @@ After VPC setup, you can follow the templates down below to setup Lambda fucntio
556
610
557
611
1. Replace the Security Group ID and Subnet ID to match your Redis' VPC setting.
558
612
559
-
### Idempotent decorator for Redis
613
+
### Configuring Redis persistence layer
560
614
561
615
You can quickly start by initializing the `RedisCachePersistenceLayer` class and using it with the `idempotent` decorator on your lambda handler. Check out detailed example of `RedisCachePersistenceLayer` in [Persistence layers section](#redispersistencelayer)
562
616
563
617
???+ warning "Passing in Redis Client"
564
-
We support passing in established Redis clients when initilizing`RedisPersistenceLayer`. However, this rely on Redis parameter `decode_responses=True` to decode all Redis response. Please make sure this parameter is set when establishing Redis client or `RedisPersistenceLayer` will raise a `IdempotencyRedisClientConfigError`. See example below
618
+
We support passing in established Redis clients when initializing`RedisPersistenceLayer`. However, this rely on Redis parameter `decode_responses=True` to decode all Redis response. Please make sure this parameter is set when establishing Redis client or `RedisPersistenceLayer` will raise a `IdempotencyRedisClientConfigError`. See example below
565
619
566
620
=== "Use established Redis Client"
567
621
```python hl_lines="4 7 12-16 18 32"
@@ -581,54 +635,39 @@ You can quickly start by initializing the `RedisCachePersistenceLayer` class and
For other use cases like `Idempotent function decorator` please reference the [DynamoDB section](#idempotent_function-decorator). You only need to substitute the `persistence_store` from `DynamoDBPersistenceLayer` to `RedisPersistenceLayer` and no other code changes are required.
585
-
586
-
## Advanced
587
-
588
-
### Persistence layers
589
-
590
-
#### DynamoDBPersistenceLayer
591
-
592
-
This persistence layer is built-in, and you can either use an existing DynamoDB table or create a new one dedicated for idempotency state (recommended).
638
+
### Custom advanced settings
593
639
594
-
=== "Customizing DynamoDBPersistenceLayer to suit your table structure"
640
+
For advanced settings, including SSL certificates and the ability to customize parameters such as a custom timeout, you can use the Redis client to accommodate these specific settings.
|**table_name**|:heavy_check_mark:|| Table name to store state |
605
-
|**key_attr**||`id`| Partition key of the table. Hashed representation of the payload (unless **sort_key_attr** is specified) |
606
-
|**expiry_attr**||`expiration`| Unix timestamp of when record expires |
607
-
|**in_progress_expiry_attr**||`in_progress_expiration`| Unix timestamp of when record expires while in progress (in case of the invocation times out) |
608
-
|**status_attr**||`status`| Stores status of the lambda execution during and after invocation |
609
-
|**data_attr**||`data`| Stores results of successfully executed Lambda handlers |
610
-
|**validation_key_attr**||`validation`| Hashed representation of the parts of the event used for validation |
611
-
|**sort_key_attr**||| Sort key of the table (if table is configured with a sort key). |
612
-
|**static_pk_value**||`idempotency#{LAMBDA_FUNCTION_NAME}`| Static value to use as the partition key. Only used when **sort_key_attr** is set. |
613
-
614
-
#### RedisPersistenceLayer
615
-
616
-
This persistence layer is built-in, and you can use an existing Redis service. We don't recomend using Redis Persistence Layer if you don't have a exsiting Redis service. You can try [DynamoDBPersistenceLayer](#dynamodbpersistencelayer) instead.
617
-
618
-
=== "Customizing RedisPersistenceLayer to suit your data structure"
|**in_progress_expiry_attr**||`in_progress_expiration`| Unix timestamp of when record expires while in progress (in case of the invocation times out) |
629
-
|**status_attr**||`status`| Stores status of the lambda execution during and after invocation |
630
-
|**data_attr**||`data`| Stores results of successfully executed Lambda handlers |
631
-
|**validation_key_attr**||`validation`| Hashed representation of the parts of the event used for validation |
670
+
## Advanced
632
671
633
672
### Customizing the default behavior
634
673
@@ -858,7 +897,7 @@ The idempotency utility can be used with the `validator` decorator. Ensure that
858
897
If you use an envelope with the validator, the event received by the idempotency utility will be the unwrapped
859
898
event - not the "raw" event Lambda was invoked with.
860
899
861
-
Make sure to account for this behaviour, if you set the `event_key_jmespath`.
900
+
Make sure to account for this behavior, if you set the `event_key_jmespath`.
862
901
863
902
=== "Using Idempotency with JSONSchema Validation utility"
0 commit comments