powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Bug: with a DynamoDB composite key, DataRecord.idempotency_key is the static partition key value

Open leandrodamascena opened this issue 1 month ago • 4 comments

[!WARNING] For contributors, human or agent. Comment here to claim this issue and wait for the assignment before opening a PR; if two PRs show up, the assignee's stays. AI-assisted work is welcome, say so in the PR and tell us how you verified it: run the tests yourself and paste what you saw, do not describe runs you did not do. Read AGENTS.md first, it has the commands and the rules the CI enforces. This goes into 3.41.0.

Expected Behaviour

With a composite key (sort_key_attr set), DataRecord.idempotency_key is the record's own key, the value stored in the sort key, so the local cache, the response_hook and the IdempotencyAlreadyInProgressError message refer to the actual record.

Current Behaviour

DynamoDBPersistenceLayer._item_to_data_record sets idempotency_key=data[self.key_attr], which with a composite key is the static partition key value (static_pk_value, idempotency#<function> by default) for every record. So use_local_cache stores every record under one key and never hits for records written elsewhere, the response_hook receives idempotency_key='idempotency#myfn' (the docs example reads that field), and the in-progress error message shows the static value.

Code snippet

from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer

layer = DynamoDBPersistenceLayer(table_name="t", sort_key_attr="sk", static_pk_value="idempotency#fn")
record = layer._item_to_data_record(
    {"id": {"S": "idempotency#fn"}, "sk": {"S": "fn#abc"}, "status": {"S": "COMPLETED"}, "expiration": {"N": "9999999999"}, "data": {"S": "{}"}},
)
record.idempotency_key  # 'idempotency#fn', expected 'fn#abc'

For the public-API test, use botocore.stub.Stubber on the layer's client like the existing composite-key tests do, and assert on what the response_hook receives.

Possible Solution

In aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py, _item_to_data_record: idempotency_key=data[self.sort_key_attr] if self.sort_key_attr else data[self.key_attr]. Add a test with sort_key_attr and a response_hook asserting the key, and one for the local cache hit.

Item 7 of the Idempotency audit in #8603. No AWS resources needed, the existing tests stub DynamoDB.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

No response

leandrodamascena avatar Oct 31 '26 22:10 leandrodamascena

Hi, I'd like to take this one. Plan: in DynamoDBPersistenceLayer._item_to_data_record, use the sort key value as idempotency_key when sort_key_attr is set. I'll add Stubber-based functional tests for the response_hook key and the local cache hit with a composite key, and confirm they fail on develop. I'm working with an AI assistant and will say so in the PR, along with the test output from my own runs.

sumedh-aerram avatar Oct 31 '26 22:10 sumedh-aerram

@sumedh-aerram it's yours, assigning you now. Go ahead with the PR, and say in the description how you ran the tests.

leandrodamascena avatar Nov 01 '26 09:11 leandrodamascena

[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

This is now released under 3.41.0 version!

github-actions[bot] avatar Nov 18 '26 12:11 github-actions[bot]