Lazy load DynamoDB Table call at idempotency wrapper
With current implementation if my python script has declared @idempotency associated with DynamoDBPersistenceLayer there is a side effect of have to be able to connect with DynamoDB to import or test anything under that python script.
This is an unnecessary side effect and, IMHO, undesired one.
We should have DynamoDBPersistenceLayer to postpone the connection to DynamoDB as long as it can.
Current Behavior
my_lambda_handler.py
from aws_lambda_powertools.utilities.idempotency import (DynamoDBPersistenceLayer, idempotent)
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
@idempotent(persistence_store=persistence_layer)
def handler(event, context):
some_result = do_some_trick()
...
return some_result
def do_some_trick():
return "hello world"
my_lambda_handler_test.py
def test_do_some_trick_should_return_hello_world():
# At this point we need to add a mock to IdempotencyTable even if we are not testing handler function
from my_lambda_handler import do_some_trick
some_other_script.py
# At runtime, there is a call to Dynamo to use a resource unrelated to the wrapped function.
from my_lambda_handler import do_some_trick
hello = do_some_trick()
Expected Behavior
We want calls to DynamoDB to be made only when needed. Meaning when the wrapped method is called. This can reduce UT verbose and improve script reuse
Possible Solution
Avoid call to self.table = self._ddb_resource.Table(self.table_name) at __init__ of class DynamoDBPersistenceLayer
Environment
- Powertools version used: v1.20.1:
- Packaging format: PyPi:
- AWS Lambda function runtime: All
PS: I have a PR almost ready to contribute to this change
Thanks for opening your first issue here! We'll come back to you as soon as we can.
There is something wrong with my analysis. Actually self._ddb_resource.Table(self.table_name) does not trigger ddb. Will reopen once get to the root cause