为什么通过 cdk 向 dynamodb table 添加二级索引需要重新创建 table?

Why does adding a secondary index to a dynamodb table via cdk requires a recreation of the table?

将二级索引添加到(以前通过部署的 cdk)dynamodb table like

table.addLocalSecondaryIndex({
  indexName: "indexName",
  sortKey: {
    name: "keyName",
    type: dynamodb.AttributeType.STRING,
  },
  projectionType: dynamodb.ProjectionType.INCLUDE,
  nonKeyAttributes: ["attr1", "attr2"],
});

需要重新创建 table,如 cdk 创建的 cloudformation 更改集中所示:

    "resourceChange": {
      "logicalResourceId": "---ID---",
      "action": "Modify",
      "physicalResourceId": "---ID---",
      "resourceType": "AWS::DynamoDB::Table",
      "replacement": "True",
      "moduleInfo": null,
      "details": [
        {
          "target": {
            "name": "LocalSecondaryIndexes",
            "requiresRecreation": "Always",
            "attribute": "Properties"
          },
          "causingEntity": null,
          "evaluation": "Static",
          "changeSource": "DirectModification"
        }
      ],
      "changeSetId": null,
      "scope": [
        "Properties"
      ]
    },
    "type": "Resource"
  }
  1. 这是为什么?
  2. 是否可以通过某种方式阻止这种情况,或者除了通过 aws-console 手动添加索引之外是否有解决方法?

干杯, 赫尔格

您误会了全局和本地二级索引。 table创建后local不能创建,只有global可以。这就是您的代码失败的原因。

Why is that?

LocalSecondaryIndexes require replacement of DynamoDB table, since LSI can only be created on the table create time. In contrast, modifications to GlobalSecondaryIndexes 的更改导致 没有中断

Can that be prevented somehow or is there a Workaround besides adding the index manually via the aws-console?

遗憾的是,如上所述,没有办法阻止这种情况。如果您不想继续更换表格,可以使用 GSI