首页 > 解决方案 > 为什么使用 CloudFormation 制作 DynamoDB 表时出现“AttributeName 不能为空”?

问题描述

我正在尝试创建两个 DynamoDB 表。两个表都只有一个分区键,一个表有一个 GSI,它带有一个分区键和一个排序键。当我尝试在 CF 中创建堆栈时,我在两个表上都收到一条错误消息,指出“Property AttributeName 不能为空。”。但是,我相信我已经为我的每个键值提供了 AttributeName。我也将模板转换为 JSON,但我得到了同样的错误。我哪里错了?请注意,这不是完整的模板,只是导致错误的部分。首先十分感谢!

YAML 配置:

DynamoDBTable:
        Type: "AWS::DynamoDB::Table"
        Properties:
            AttributeDefinitions: 
              - 
                AttributeName: "eventName"
                AttributeType: "S"
            TableName: "BlockCursorTable"
            Tags: 
              - 
                Key: "project"
                Value: "flow-event-monitor"
            KeySchema: 
              - 
                AttributeName: "eventName"
                KeyType: "HASH"
            ProvisionedThroughput: 
                ReadCapacityUnits: 1
                WriteCapacityUnits: 1
            TimeToLiveSpecification: 
                Enabled: false

    DynamoDBTable2:
        Type: "AWS::DynamoDB::Table"
        Properties:
            AttributeDefinitions: 
              - 
                AttributeName: "listingResourceID"
                AttributeType: "N"
              - 
                AttributeName: "staticKey"
                AttributeType: "N"
              - 
                AttributeName: "timestamp"
                AttributeType: "S"
            TableName: "ListingTable"
            Tags: 
              - 
                Key: "project"
                Value: "flow-event-monitor"
            KeySchema: 
              - 
                AttributeName: "listingResourceID"
                KeyType: "HASH"
            ProvisionedThroughput: 
                ReadCapacityUnits: 1
                WriteCapacityUnits: 1
            GlobalSecondaryIndexes: 
              - 
                IndexName: "staticKey-timestamp-index"
                KeySchema: 
                  - 
                    AttributeName: "staticKey"
                    KeyType: "HASH"
                  - 
                    AttributeName: "timestamp"
                    KeyType: "RANGE"
                Projection: 
                    ProjectionType: "ALL"
                ProvisionedThroughput: 
                    ReadCapacityUnits: 1
                    WriteCapacityUnits: 1
            TimeToLiveSpecification: 
                Enabled: false

等效的 JSON 模板:

{
  "DynamoDBTable": {
    "Type": "AWS::DynamoDB::Table",
    "Properties": {
      "AttributeDefinitions": [
        {
          "AttributeName": "eventName",
          "AttributeType": "S"
        }
      ],
      "TableName": "BlockCursorTable",
      "Tags": [
        {
          "Key": "project",
          "Value": "flow-event-monitor"
        }
      ],
      "KeySchema": [
        {
          "AttributeName": "eventName",
          "KeyType": "HASH"
        }
      ],
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 1,
        "WriteCapacityUnits": 1
      },
      "TimeToLiveSpecification": {
        "Enabled": false
      }
    }
  },
  "DynamoDBTable2": {
    "Type": "AWS::DynamoDB::Table",
    "Properties": {
      "AttributeDefinitions": [
        {
          "AttributeName": "listingResourceID",
          "AttributeType": "N"
        },
        {
          "AttributeName": "staticKey",
          "AttributeType": "N"
        },
        {
          "AttributeName": "timestamp",
          "AttributeType": "S"
        }
      ],
      "TableName": "ListingTable",
      "Tags": [
        {
          "Key": "project",
          "Value": "flow-event-monitor"
        }
      ],
      "KeySchema": [
        {
          "AttributeName": "listingResourceID",
          "KeyType": "HASH"
        }
      ],
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 1,
        "WriteCapacityUnits": 1
      },
      "GlobalSecondaryIndexes": [
        {
          "IndexName": "staticKey-timestamp-index",
          "KeySchema": [
            {
              "AttributeName": "staticKey",
              "KeyType": "HASH"
            },
            {
              "AttributeName": "timestamp",
              "KeyType": "RANGE"
            }
          ],
          "Projection": {
            "ProjectionType": "ALL"
          },
          "ProvisionedThroughput": {
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
          }
        }
      ],
      "TimeToLiveSpecification": {
        "Enabled": false
      }
    }
  }
}

标签: amazon-web-servicesyamlamazon-dynamodbamazon-cloudformation

解决方案


您为两个表指定 a TimeToLiveSpecificationwith no ,这是AWS 文档所必需的。AttributeName

该属性存在的原因Enabled实际上是当您需要AttributeName已启用的 TTL更新时- 在这种情况下,该Enabled字段用于首先禁用 TTL,然后允许您AttributeName在重新启用的同时更改TTL。

在您的情况下,您希望禁用 TTL。

默认情况下禁用生存时间,因此请随意从 CloudFormation 模板中删除这些部分,因为它们不起作用。

我在Resources下面包含了一个部分以允许您进行测试(考虑到您只共享了模板的一部分)。

这应该有效:

{
  "Resources":{
    "DynamoDBTable":{
      "Type":"AWS::DynamoDB::Table",
      "Properties":{
        "AttributeDefinitions":[
          {
            "AttributeName":"eventName",
            "AttributeType":"S"
          }
        ],
        "TableName":"BlockCursorTable",
        "Tags":[
          {
            "Key":"project",
            "Value":"flow-event-monitor"
          }
        ],
        "KeySchema":[
          {
            "AttributeName":"eventName",
            "KeyType":"HASH"
          }
        ],
        "ProvisionedThroughput":{
          "ReadCapacityUnits":1,
          "WriteCapacityUnits":1
        }
      }
    },
    "DynamoDBTable2":{
      "Type":"AWS::DynamoDB::Table",
      "Properties":{
        "AttributeDefinitions":[
          {
            "AttributeName":"listingResourceID",
            "AttributeType":"N"
          },
          {
            "AttributeName":"staticKey",
            "AttributeType":"N"
          },
          {
            "AttributeName":"timestamp",
            "AttributeType":"S"
          }
        ],
        "TableName":"ListingTable",
        "Tags":[
          {
            "Key":"project",
            "Value":"flow-event-monitor"
          }
        ],
        "KeySchema":[
          {
            "AttributeName":"listingResourceID",
            "KeyType":"HASH"
          }
        ],
        "ProvisionedThroughput":{
          "ReadCapacityUnits":1,
          "WriteCapacityUnits":1
        },
        "GlobalSecondaryIndexes":[
          {
            "IndexName":"staticKey-timestamp-index",
            "KeySchema":[
              {
                "AttributeName":"staticKey",
                "KeyType":"HASH"
              },
              {
                "AttributeName":"timestamp",
                "KeyType":"RANGE"
              }
            ],
            "Projection":{
              "ProjectionType":"ALL"
            },
            "ProvisionedThroughput":{
              "ReadCapacityUnits":1,
              "WriteCapacityUnits":1
            }
          }
        ]
      }
    }
  }
}

推荐阅读