Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFormation - Enable TTL for DynamoDB Create Table

I'd like to enable TTL for my newly-created-table via CloudFormation. I've tried the following to no avail:

{
  "Resources" : {
    "mytable" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "TableName" : "my_table",
        "ProvisionedThroughput" : {"ReadCapacityUnits" : 1, "WriteCapacityUnits" : 5},
        "KeySchema" :
        [
          {"AttributeName" : "user_email", "KeyType" : "HASH"},
          {"AttributeName" : "datetime", "KeyType" : "RANGE"}
        ],
        "AttributeDefinitions": [
          {"AttributeName" : "user_email", "AttributeType" : "S"},
          {"AttributeName" : "datetime", "AttributeType" : "S"}
        ],
        "TimeToLiveDescription": {
          "AttributeName": "expire_at", 
          "TimeToLiveStatus": "ENABLED"
        }
      }
    }
}

I used the TimeToLiveDescription, which I obtained from this doc.

Attempting to create the stack gave me the following error:

Encountered unsupported property TimeToLiveDescription
like image 536
azizj Avatar asked Mar 19 '17 17:03

azizj


2 Answers

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"
  }
}
like image 92
manum Avatar answered Oct 29 '22 10:10

manum


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.

like image 44
Sudhanshu Mishra Avatar answered Oct 29 '22 09:10

Sudhanshu Mishra