在 dynamodb 资源中使用新的非关键属性更新到 GSI 的 cloudformation 模板,而无需删除 GSI 并重新添加它

问题描述

我想通过 cloudformation 将新的非关键属性更新到现有的 GSI。目前,当我想添加新的非密钥属性时,我需要取消配置 GSI 并使用新的和现有的非密钥属性重新配置它。有没有办法在不每次都取消配置 GSI 的情况下添加非关键属性

资源:

DynamoDBTable:

Type: "AWS::DynamoDB::Table"
Properties:
  TableName: "employee table"
  AttributeDeFinitions:
    - AttributeName: "CustomerId"
      AttributeType: "S"
    - AttributeName: "empId"
      AttributeType: "S"
    - AttributeName: "Date"
      AttributeType: "N"
  KeySchema:
    - AttributeName: "CustomerId"
      KeyType: "HASH"
  ProvisionedThroughput:
    ReadCapacityUnits: 20
    WriteCapacityUnits: 20
  GlobalSecondaryIndexes:
    - IndexName: "ByempId"
      KeySchema:
      - AttributeName: "empId"
        KeyType: "HASH"
      - AttributeName: "Date"
        KeyType: "RANGE"
      Projection:
        NonKeyAttributes:
          - "status1"
          - "status2"
          - "status3"
        ProjectionType: INCLUDE
      ProvisionedThroughput:
        ReadCapacityUnits: "20"
        WriteCapacityUnits: "20"
        

如果我想添加新的 NonKeyAttributes status4,我需要注释掉从 GlobalSecondaryIndexes 到模板末尾的行,并提供所有 NonKeyAttributes (status1,2,3,4)

解决方法

不支持更新 GSI,如docs 中所述:

不支持更新。以下是例外情况:

如果仅更新全局二级索引的预配置吞吐量值,则可以不间断地更新表。

您可以不间断地删除或添加一个全局二级索引。如果您在同一个更新中同时执行这两项操作(例如,通过更改索引的逻辑 ID),更新将失败。

如您所见,您可以删除和创建新 GSI,但不能更新现有 GSI。