Skip to content

DynamoDBPersistenceLayer should use ExpressionAttributeNames in _put_record #693

Closed
@Tankanow

Description

@Tankanow

What were you trying to accomplish?

Expected Behavior

I should be able to use DynamoDB reserved words as Item attributes. For example, ttl is a common DynamoDB Item Time to Live attribute name. A consumer should be able to specify reserved word attribute names for any of the *_attr values when configuring DynamoDBPersistenceLayer.

Current Behavior

_put_record currently throws an exception b/c it uses Python string interpolation instead of ExpressionAttributeNames.

Possible Solution

Switch to ExpressionAttributeNames, e.g.

            self.table.put_item(
                Item=item,
                ConditionExpression='attribute_not_exists(#key) OR #now < :now',
                ExpressionAttributeNames={"#key": self.key_attr, "#now": self.expiry_attr},
                ExpressionAttributeValues={":now": int(now.timestamp())},
            )

Steps to Reproduce (for bugs)

  1. Create a DynamoDB table with a Time to Live attribute called ttl.
  2. Configure a DynamoDBPersistenceLayer with expiry_attr='ttl'
  3. Run

Environment

  • Powertools version used: 1.20.2
  • Packaging format (Layers, PyPi): PyPi
  • AWS Lambda function runtime: Python 3.8
  • Debugging logs

How to enable debug mode**

# paste logs here

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions