Troposphere DynamoDB TimeToLiveSpecification

335 Views Asked by At

I am trying to create a JSON Cloudformation template for a new DynamoDB table. I am trying to set the TimeToLiveSpecification but I get errors and the Troposphere documentation isn't clear.

I have

dynamoDB = t.add_resource(Table(
    "myDynamoTable",
    TableName=Join("-", [Ref(env), "dynamo-table"]),
    AttributeDefinitions=[
        AttributeDefinition(
            AttributeName=Ref(hashkeyname),
            AttributeType=Ref(hashkeytype)
        ),
        AttributeDefinition(
            AttributeName="sqsMessageId",
            AttributeType="S"
        ),
        AttributeDefinition(
            AttributeName="system",
            AttributeType="S"
        ),`enter code here`
        AttributeDefinition(
            AttributeName=Ref(sortkeyname),
            AttributeType=Ref(sortkeytype)
        ),
        AttributeDefinition(
            AttributeName="text",
            AttributeType="S"
        ),
        AttributeDefinition(
            AttributeName="ttl",
            AttributeType="N"
        )
    ],
    KeySchema=[
        KeySchema(
            AttributeName=Ref(hashkeyname),
            KeyType="HASH"
        ),
        KeySchema(
            AttributeName=Ref(sortkeyname),
            KeyType="RANGE"
        )
    ],
    TimeToLiveSpecification="WHAT GOES HERE???"
))

I have tried even putting this in as ready formatted JSON, it won't work. I have tried:

TimeToLiveSpecification=AWSProperty(AttributeName="ttl", Enabled=True)

TimeToLiveSpecification=AttributeDefinition(AttributeName="ttl", Enabled=True)

TimeToLiveSpecification=TimeToLiveSchema(AttributeName="ttl", Enabled=True) (grasping at straws with this one).

2

There are 2 best solutions below

0
On BEST ANSWER

In the end I've gone for

ttlspec = t.add_resource(TimeToLiveSpecification(
    "ttlspec",
    AttributeName="ttl",
    Enabled=True
))

then

dynamoDB = t.add_resource(Table(
    "myDynamoTable",
    TableName=Join("-", [Ref(env), "dynamo-table"]),
    AttributeDefinitions=[
        AttributeDefinition(
            AttributeName=Ref(hashkeyname),
            AttributeType=Ref(hashkeytype)
        ),
        AttributeDefinition(
            AttributeName="sqsMessageId",
            AttributeType="S"
        ),
        AttributeDefinition(
            AttributeName="system",
            AttributeType="S"
        ),`enter code here`
        AttributeDefinition(
            AttributeName=Ref(sortkeyname),
            AttributeType=Ref(sortkeytype)
        ),
        AttributeDefinition(
            AttributeName="text",
            AttributeType="S"
        ),
        AttributeDefinition(
            AttributeName="ttl",
            AttributeType="N"
        )
    ],
    KeySchema=[
        KeySchema(
            AttributeName=Ref(hashkeyname),
            KeyType="HASH"
        ),
        KeySchema(
            AttributeName=Ref(sortkeyname),
            KeyType="RANGE"
        )
    ],
    TimeToLiveSpecification=Ref(ttlspec)
))

TimeToLiveSpecification is a Class and needs to be imported at the top. Docs here.

3
On

Try this (untested) with the TimeToLiveSpecification shown here:

TimeToLiveSpecification=TimeToLiveSpecification(
    AttributeName="fill this in",
    Enabled=True,
),