amazon-web-services - 使用 CloudFormation 创建 DynamoDB 表时为什么会收到 "AttributeName cannot be empty"?

标签 amazon-web-services yaml amazon-dynamodb aws-cloudformation

我正在尝试创建两个 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
      }
    }
  }
}

最佳答案

您为两个表指定一个 TimeToLiveSpecification,但没有 AttributeName,即 required per AWS docs.

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

就您而言,您希望禁用 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
            }
          }
        ]
      }
    }
  }
}

关于amazon-web-services - 使用 CloudFormation 创建 DynamoDB 表时为什么会收到 "AttributeName cannot be empty"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69506354/

相关文章:

amazon-web-services - AWS EKS K8s Service 和 CronJob/Jon 同节点

amazon-web-services - 在 DynamoDB 中查找平均值和总和?

JavaScript - 使用 AWS SES 发送电子邮件

amazon-web-services - AWS Cloudformation ELB 运行状况检查 (verify_instance_health) 不适用于 Ubuntu

service - Kubernetes:kube-dns 服务创建

kubernetes - 从 kubernetes yaml 定义中的文件创建配置映射时 |+ 和 |- 之间有什么区别?

symfony - 为ElasticSearch配置分析

amazon-dynamodb - DynamoDB 嵌套查询支持

amazon-dynamodb - DynamoDB - 它可以用于没有自然分区键的数据吗?

amazon-web-services - "no basic auth credentials"尝试从私有(private) ECR 中提取图像时