Skip to content

Bug: Idempotency DynamoDB _put_record method overwrites static_pk_value #1968

Closed
@Tankanow

Description

@Tankanow

Expected Behaviour

When I pass a static_pk_value value into the DynamoDBPersistenceLayer, I expect that value to be used in the Primary Key.

Current Behaviour

The PK value is overwritten with data_record.idempotency_key.

Code snippet

[This test](https://github.com/Tankanow/aws-lambda-powertools-python/pull/1/files) shows the problem. Running this test results in this failure:


E           aws_lambda_powertools.utilities.idempotency.exceptions.IdempotencyPersistenceLayerError: Failed to save in progress record to idempotency store - (Error getting response stub for operation PutItem: Expected parameters:
E           {'ConditionExpression': 'attribute_not_exists(#id) OR #expiry < :now OR '
E                                   '(#status = :inprogress AND '
E                                   'attribute_exists(#in_progress_expiry) AND '
E                                   '#in_progress_expiry < :now_in_millis)',
E            'ExpressionAttributeNames': {'#expiry': 'expiration',
E                                         '#id': 'id',
E                                         '#in_progress_expiry': 'in_progress_expiration',
E                                         '#status': 'status'},
E            'ExpressionAttributeValues': {':inprogress': {'S': 'INPROGRESS'},
E                                          ':now': {'N': <ANY>},
E                                          ':now_in_millis': {'N': <ANY>}},
E            'Item': {'expiration': {'N': <ANY>},
E                     'id': {'S': 'static-value'},
E                     'in_progress_expiration': {'N': <ANY>},
E                     'sk': {'S': 'test-func.functional.idempotency.test_idempotency.test_idempotent_lambda_compound_static_pk_value_has_correct_pk.<locals>.lambda_handler#be4755f20c4e6f94f43bc3f5849a26fb'},
E                     'status': {'S': 'INPROGRESS'}},
E            'TableName': 'TEST_TABLE'},
E           but received:
E           {'ConditionExpression': 'attribute_not_exists(#id) OR #expiry < :now OR '
E                                   '(#status = :inprogress AND '
E                                   'attribute_exists(#in_progress_expiry) AND '
E                                   '#in_progress_expiry < :now_in_millis)',
E            'ExpressionAttributeNames': {'#expiry': 'expiration',
E                                         '#id': 'id',
E                                         '#in_progress_expiry': 'in_progress_expiration',
E                                         '#status': 'status'},
E            'ExpressionAttributeValues': {':inprogress': {'S': 'INPROGRESS'},
E                                          ':now': {'N': '1677689099'},
E                                          ':now_in_millis': {'N': '1677689099925'}},
E            'Item': {'expiration': {'N': '1677692699'},
E                     'id': {'S': 'test-func.functional.idempotency.test_idempotency.test_idempotent_lambda_compound_static_pk_value_has_correct_pk.<locals>.lambda_handler#be4755f20c4e6f94f43bc3f5849a26fb'},
E                     'in_progress_expiration': {'N': '1677689100925'},
E                     'sk': {'S': 'test-func.functional.idempotency.test_idempotency.test_idempotent_lambda_compound_static_pk_value_has_correct_pk.<locals>.lambda_handler#be4755f20c4e6f94f43bc3f5849a26fb'},
E                     'status': {'S': 'INPROGRESS'}},
E            'TableName': 'TEST_TABLE'})

Possible Solution

Simply remove the offending line, self.key_attr: {"S": data_record.idempotency_key}, in https://github.com/Tankanow/aws-lambda-powertools-python/blob/5f9addda12d3c4066fe7f480f99184126b154d57/aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py#L149.

Steps to Reproduce

Run the tests in this PR

I will happily fix if this issue is accepted.

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

PyPi

Debugging logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingidempotencyIdempotency utility

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions