Skip to content

Commit c706b0c

Browse files
committed
feat(idempotench): add mechanism to register lambda context
1 parent fa80aed commit c706b0c

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

aws_lambda_powertools/utilities/idempotency/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(
7474
self.data = deepcopy(_prepare_data(function_payload))
7575
self.fn_args = function_args
7676
self.fn_kwargs = function_kwargs
77+
self.config = config
7778

7879
persistence_store.configure(config, self.function.__name__)
7980
self.persistence_store = persistence_store
@@ -129,6 +130,10 @@ def _get_remaining_time_in_millis(self) -> Optional[int]:
129130
Remaining time in millis, or None if the remaining time cannot be determined.
130131
"""
131132

133+
# Look to see if we have stored a Lambda Context
134+
if self.config.lambda_context is not None:
135+
self.config.lambda_context.get_remainig_time_in_millis()
136+
132137
# Look into fn_args to see if we have a lambda context
133138
if self.fn_args and len(self.fn_args) == 2 and getattr(self.fn_args[1], "get_remaining_time_in_millis", None):
134139
return self.fn_args[1].get_remaining_time_in_millis()

aws_lambda_powertools/utilities/idempotency/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import Dict, Optional
22

3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
35

46
class IdempotencyConfig:
57
def __init__(
@@ -41,3 +43,7 @@ def __init__(
4143
self.use_local_cache = use_local_cache
4244
self.local_cache_max_items = local_cache_max_items
4345
self.hash_function = hash_function
46+
self.lambda_context = None
47+
48+
def register_lambda_context(self, lambda_context: LambdaContext):
49+
self.lambda_context = lambda_context

docs/utilities/idempotency.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,6 @@ after this timestamp, and the record is still marked `INPROGRESS`, we execute th
400400
already expired. This means that if an invocation expired during execution, it will be quickly executed again on the
401401
next retry.
402402

403-
???+ info "Info: Calculating the remaining available time"
404-
For now this only works with the `idempotent` decorator. At the moment we
405-
don't have access to the Lambda context when using the
406-
`idempotent_function` so enabling this option is a no-op in that scenario.
407-
408403
<center>
409404
```mermaid
410405
sequenceDiagram
@@ -439,6 +434,32 @@ sequenceDiagram
439434
<i>Idempotent sequence for Lambda timeouts</i>
440435
</center>
441436

437+
???+ info "Info: Calculating the remaining available time"
438+
When using the `idempotent` decorator, we captura and calculate the remaining available time for you.
439+
However, when using the `idempotent_function`, the functionality doesn't work out of the box. You'll
440+
need to register the Lambda context on your handler:
441+
442+
```python hl_lines="8 16" title="Registering the Lambda context"
443+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
444+
from aws_lambda_powertools.utilities.idempotency import (
445+
IdempotencyConfig, idempotent_function
446+
)
447+
448+
persistence_layer = DynamoDBPersistenceLayer(table_name="...")
449+
450+
config = IdempotencyConfig()
451+
452+
@idempotent_function(data_keyword_argument="record", persistence_store=persistence_layer, config=config)
453+
def record_handler(record: SQSRecord):
454+
return {"message": record["body"]}
455+
456+
457+
def lambda_handler(event, context):
458+
config.register_lambda_context(context)
459+
460+
return record_handler(event)
461+
```
462+
442463
### Handling exceptions
443464

444465
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**.

0 commit comments

Comments
 (0)