CloudFormation - Enable TTL for DynamoDB Create Table

For the sake of completeness, following is an example CloudFromation YAML that creates a Table with TTL enabled

AWSTemplateFormatVersion: '2010-09-09'
Description: The database for my Service

Resources:
  BatchDataTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: "MyDynamoTable"
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: Id
          AttributeType: S
        - AttributeName: SortKey
          AttributeType: S
      KeySchema: 
        - AttributeName: Id
          KeyType: "HASH"
        - AttributeName: SortKey
          KeyType: "RANGE"           
      TimeToLiveSpecification:
        AttributeName: TimeToLive
        Enabled: true

Now if you add an item to this tabe with an Attribute named "TimeToLive" and have it's value set to the Unix Epoch at which the item should expire, DynamoDB will clear the item from that table when the TTL is reached.


The DynamoDB TTL-support for CloudFormation exists now. See:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-timetolivespecification.html

Example:

{
  "TableName": "MyTable",
  "AttributeDefinitions": [
    {
      "AttributeName": "Uid",
      "AttributeType": "S"
    }
  ],
  "KeySchema": [
    {
      "AttributeName": "Uid",
      "KeyType": "HASH"
    }
  ],
  "ProvisionedThroughput": {
    "ReadCapacityUnits": "1",
    "WriteCapacityUnits": "1"
  },
  "TimeToLiveSpecification": {
    "AttributeName": "TimeToLive",
    "Enabled": "TRUE"
  }
}