【问题标题】:CloudFormation Template for dynamoDB won't work: One or more parameters were invaliddynamoDB 的 CloudFormation 模板不起作用:一个或多个参数无效
【发布时间】:2021-05-07 04:28:46
【问题描述】:

我正在尝试为我的每个表格构建一个模板,并且我正在使用一个表格对其进行测试。但是,我收到了这个错误,我不知道如何解决它:One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: NRTF448TGFEUGUB3H5UH37AGHBVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)

我使用的模板是这个:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Resources" : {
      "myddbtable": {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions" : [ 
          {
            "AttributeName": "phoneNumber",
            "AttributeType": "S" 
          },
          {
            "AttributeName": "fname",
            "AttributeType": "S" 
          },
          {
            "AttributeName": "lname",
            "AttributeType": "S" 
          }
          ],
        "BillingMode" : "PAY_PER_REQUEST",
        "KeySchema" : [
          {
            "AttributeName" : "phoneNumber",
            "KeyType": "HASH"
          }
        ],
        "TableName" : "UserTable2"
      }
    }
  }
}

我尝试将 fname 和 lname 属性放在 KeySchema 部分中,KeyType 为 Range,但它也不起作用,有什么想法吗?

【问题讨论】:

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

由于 DynamoDB 没有架构,我们可以在创建/更新单个记录时添加任何属性。因此,在创建 DynamoDB 表时,我们只需要定义属于主索引或全局二级索引的属性即可。

在这种情况下,表只有phoneNumber 的主键。我们需要从定义中删除另外两个属性 fname 和 'lname`

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "myddbtable": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "AttributeDefinitions": [
          {
            "AttributeName": "phoneNumber",
            "AttributeType": "S"
          }
        ],
        "BillingMode": "PAY_PER_REQUEST",
        "KeySchema": [
          {
            "AttributeName": "phoneNumber",
            "KeyType": "HASH"
          }
        ],
        "TableName": "UserTable2"
      }
    }
  }
}

【讨论】:

  • 但这样我将不得不手动定义所有其他字段,而不是在那里描述它们。有没有办法添加这样的属性?
  • @DiegoDuarte 有这样的方法,因为 DdB 没有任何架构,除非您开始添加本地或全局二级索引。但如果需要架构,则应考虑使用常规数据库,例如 RDS。
  • @Marcin 好吧。这就说得通了。我想我必须坚持下去,然后在 lambda 函数上添加任何其他字段。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多