属性定义不一致


73

我在Cloudformation UI中使用以下模板来创建dynamoDB表。我想创建一个表,其中PrimaryKey作为IDsortKey作为Value

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions": [ { 
          "AttributeName" : "ID",
          "AttributeType" : "S"
        }, { 
          "AttributeName" : "Value",
          "AttributeType" : "S"
        } ],
        "KeySchema": [
          { 
            "AttributeName": "ID", 
            "KeyType": "HASH"
          }
        ]                
      },
      "TableName": "TableName"
    }
  }
}

在CF UI上,我单击新堆栈,指向template本地计算机上的文件,为堆栈命名,然后单击下一步。一段时间后,我收到错误消息,指出属性AttributeDefinitions与表的KeySchema和辅助索引不一致



CloudFormation Linter规则可帮助您更快地捕获更多信息:github.com/aws-cloudformation/cfn-python-lint/pull/1284
Pat Myron

Answers:


123

问题是Resources.Properties.AttributeDefinitions键必须定义用于索引或键的列。换句话说,中的键Resources.Properties.AttributeDefinitions必须匹配中定义的相同键Resources.Properties.KeySchema

AWS文档:

AttributeDefinitions:AttributeName和AttributeType对象的列表,这些对象描述表和索引的键架构。

因此生成的模板如下所示:

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
    "Type" : "AWS::DynamoDB::Table",
    "Properties" : {
      "AttributeDefinitions": [ { 
        "AttributeName" : "ID",
        "AttributeType" : "S"
      } ],
      "ProvisionedThroughput":{
        "ReadCapacityUnits" : 1,
        "WriteCapacityUnits" : 1
      },
      "KeySchema": [
        { 
          "AttributeName": "ID", 
          "KeyType": "HASH"
        }
       ] ,               
      "TableName": "table5"
    }
   }
  }
}

3
如果从AttributeDefinitions中删除“值”属性,如何将“值”列添加到表中?
Dilantha

28
stackoverflow.com/questions/25606135/…经过研究,创建表时无需定义所有列,只需定义索引,然后在插入新行时就可以“随时随地”添加属性
ThomasP1988


该问题指出Value应该是排序键,因此应将其包含在中KeySchema,而不是从中删除AttributeDefinitions
詹森·沃兹沃思

1

接受的答案是导致错误的原因,但是您说您希望排序键是Value。因此,您应该更改CloudFormation使其包含以下内容:

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions": [ { 
          "AttributeName" : "ID",
          "AttributeType" : "S"
        }, { 
          "AttributeName" : "Value",
          "AttributeType" : "S"
        } ],
        "KeySchema": [
          { 
            "AttributeName": "ID", 
            "KeyType": "HASH"
          },
          { 
            "AttributeName": "Value", 
            "KeyType": "RANGE"
          }
        ]                
      },
      "TableName": "TableName"
    }
  }
}

0

在AttributeDefinitions中,您仅需要定义分区键和范围键,而无需定义其他属性。

AttributeDefinitions和KeySchema中的属性数量应该匹配并且应该完全相同。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.