Skip to content

Commit 88e983e

Browse files
author
Tom McCarthy
committed
fix: Allow event_key_jmespath to be left empty to use entire event as payload
1 parent 43b72e7 commit 88e983e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

aws_lambda_powertools/utilities/idempotency/persistence/base.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class BasePersistenceLayer(ABC):
114114

115115
def __init__(
116116
self,
117-
event_key_jmespath: str,
117+
event_key_jmespath: str = "",
118118
payload_validation_jmespath: str = "",
119119
expires_after_seconds: int = 60 * 60, # 1 hour default
120120
use_local_cache: bool = False,
@@ -140,7 +140,8 @@ def __init__(
140140
Function to use for calculating hashes, by default md5.
141141
"""
142142
self.event_key_jmespath = event_key_jmespath
143-
self.event_key_compiled_jmespath = jmespath.compile(event_key_jmespath)
143+
if self.event_key_jmespath:
144+
self.event_key_compiled_jmespath = jmespath.compile(event_key_jmespath)
144145
self.expires_after_seconds = expires_after_seconds
145146
self.use_local_cache = use_local_cache
146147
if self.use_local_cache:
@@ -166,7 +167,9 @@ def _get_hashed_idempotency_key(self, lambda_event: Dict[str, Any]) -> str:
166167
Hashed representation of the data extracted by the jmespath expression
167168
168169
"""
169-
data = self.event_key_compiled_jmespath.search(lambda_event)
170+
data = lambda_event
171+
if self.event_key_jmespath:
172+
data = self.event_key_compiled_jmespath.search(lambda_event)
170173
return self._generate_hash(data)
171174

172175
def _get_hashed_payload(self, lambda_event: Dict[str, Any]) -> str:

docs/utilities/idempotency.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ Resources:
6666
### Lambda handler
6767
6868
You can quickly start by initializing the `DynamoDBPersistenceLayer` class outside the Lambda handler, and using it
69-
with the `idempotent` decorator on your lambda handler. There are 2 required parameters to initialize the persistence
70-
layer:
69+
with the `idempotent` decorator on your lambda handler. The only required parameter is `table_name`, but you likely
70+
want to specify `event_key_jmespath` as well.
7171

72-
`table_name`: The name of the DynamoDB table to use.
7372
`event_key_jmespath`: A JMESpath expression which will be used to extract the payload from the event your Lambda hander
74-
is called with. This payload will be used as the key to decide if future invocations are duplicates.
73+
is called with. This payload will be used as the key to decide if future invocations are duplicates. If you don't pass
74+
this parameter, the entire event will be used as the key.
7575

7676
=== "app.py"
7777

0 commit comments

Comments
 (0)