azizj
azizj

Reputation: 3777

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

Upvotes: 14

Views: 22984

Answers (4)

manum
manum

Reputation: 308

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"
  }
}

Upvotes: 25

Sudhanshu Mishra
Sudhanshu Mishra

Reputation: 6733

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.

Upvotes: 22

ivanji
ivanji

Reputation: 187

TTL for AWS Dynamo DB is a new feature (launched in Feb 2017) and like Jared mentioned in his answer, it doesn't seem to be supported by AWS Cloudformation just yet. In the meantime, what you could do - if you're launching a new EC2 instance in the same cloudformation template - is to execute (under UserData) the aws cli command you linked to which, would update the TTL aws dynamodb update-time-to-live --table-name TTLExample --time-to-live-specification "Enabled=true, AttributeName=ttl", making a reference to your dynamo db resource (mytable). (Also make sure the instance is using an IAM role with the necessary policies to be able to update this resource).

Upvotes: 3

JaredHatfield
JaredHatfield

Reputation: 6651

That link is an example for the AWS CLI.

Support for configuring DynamoDB TTLs has not been added to cloudformation yet.

Upvotes: 0

Related Questions