diff --git a/aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py b/aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py index ae3a1be490f..73f241bd613 100644 --- a/aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py +++ b/aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py @@ -121,7 +121,8 @@ def _put_record(self, data_record: DataRecord) -> None: logger.debug(f"Putting record for idempotency key: {data_record.idempotency_key}") self.table.put_item( Item=item, - ConditionExpression=f"attribute_not_exists({self.key_attr}) OR {self.expiry_attr} < :now", + ConditionExpression="attribute_not_exists(#id) OR #now < :now", + ExpressionAttributeNames={"#id": self.key_attr, "#now": self.expiry_attr}, ExpressionAttributeValues={":now": int(now.timestamp())}, ) except self._ddb_resource.meta.client.exceptions.ConditionalCheckFailedException: diff --git a/tests/functional/idempotency/conftest.py b/tests/functional/idempotency/conftest.py index 2c528cafc50..f563b4bbcda 100644 --- a/tests/functional/idempotency/conftest.py +++ b/tests/functional/idempotency/conftest.py @@ -122,7 +122,8 @@ def expected_params_update_item_with_validation( @pytest.fixture def expected_params_put_item(hashed_idempotency_key): return { - "ConditionExpression": "attribute_not_exists(id) OR expiration < :now", + "ConditionExpression": "attribute_not_exists(#id) OR #now < :now", + "ExpressionAttributeNames": {"#id": "id", "#now": "expiration"}, "ExpressionAttributeValues": {":now": stub.ANY}, "Item": {"expiration": stub.ANY, "id": hashed_idempotency_key, "status": "INPROGRESS"}, "TableName": "TEST_TABLE", @@ -132,7 +133,8 @@ def expected_params_put_item(hashed_idempotency_key): @pytest.fixture def expected_params_put_item_with_validation(hashed_idempotency_key, hashed_validation_key): return { - "ConditionExpression": "attribute_not_exists(id) OR expiration < :now", + "ConditionExpression": "attribute_not_exists(#id) OR #now < :now", + "ExpressionAttributeNames": {"#id": "id", "#now": "expiration"}, "ExpressionAttributeValues": {":now": stub.ANY}, "Item": { "expiration": stub.ANY,