CloudFormation - 为DynamoDB创建表启用TTL

Azi*_*ved 4 amazon-web-services aws-cloudformation amazon-dynamodb

我想通过CloudFormation为我新创建的表启用TTL.我试过以下无济于事:

{
  "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"
        }
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用了TimeToLiveDescription,这是我从这个doc获得的.

尝试创建堆栈给了我以下错误:

Encountered unsupported property TimeToLiveDescription
Run Code Online (Sandbox Code Playgroud)

Sud*_*hra 16

为完整起见,以下是创建启用 TTL 的表的 CloudFromation YAML 示例

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
Run Code Online (Sandbox Code Playgroud)

现在,如果您使用名为“TimeToLive”的属性将一个项目添加到此选项卡,并将其值设置为该项目应到期的 Unix Epoch,DynamoDB 将在达到 TTL 时从该表中清除该项目。


小智 12

CloudFormation的DynamoDB TTL支持现在存在.看到:

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

例:

{
  "TableName": "MyTable",
  "AttributeDefinitions": [
    {
      "AttributeName": "Uid",
      "AttributeType": "S"
    }
  ],
  "KeySchema": [
    {
      "AttributeName": "Uid",
      "KeyType": "HASH"
    }
  ],
  "ProvisionedThroughput": {
    "ReadCapacityUnits": "1",
    "WriteCapacityUnits": "1"
  },
  "TimeToLiveSpecification": {
    "AttributeName": "TimeToLive",
    "Enabled": "TRUE"
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我不明白这个例子。意味着该项目必须具有名为“TimeToLive”且属性类型为“N”的属性?您能否提供 dynamoDB.putItem() 方法的示例? (2认同)