使用键模式上不存在的属性创建 table 时出现问题

Issue when creating a table with attributes non present on keyschema

我正在学习 DynamoDB。

我想用以下主键创建一个 table:

并具有以下属性:

这是我的 TS 界面(以防它有助于描述我想要的东西)

interface User {
  readonly name: string;
  readonly address: string;
  readonly creationDate: Date;
  readonly isActive: boolean;
}

我编写了以下模板:

Resources:
  myDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
      - AttributeName: 'name'
        AttributeType: 'S'
      - AttributeName: 'address'
        AttributeType: 'S'
      - AttributeName: 'creationDate'
        AttributeType: 'S'
      - AttributeName: 'isActive'
        AttributeType: 'S'
      KeySchema:
      - AttributeName: 'name'
        KeyType: HASH
      - AttributeName: 'creationDate'
        KeyType: RANGE

当我部署它时,它失败并出现以下错误

One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions

我 google 它并找到了几个答案,但我仍然不明白发生了什么以及如何在我的 table 中拥有这些属性(所有项目都将具有这些属性)。

有什么办法可以做到吗?如果是这样,我做错了什么? 谢谢

您的 AttributeDefinitions 中不能有 isActiveaddress,因为它们不在您的 KeySchema 中。您还缺少 ProvisionedThroughput。所以正确的 table 定义是(例子):

Resources:
  myDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
      - AttributeName: 'name'
        AttributeType: 'S'
      - AttributeName: 'creationDate'
        AttributeType: 'S'
      KeySchema:
      - AttributeName: 'name'
        KeyType: HASH
      - AttributeName: 'creationDate'
        KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1