【问题标题】:AWS SAM recreates DynamoDB Table when adding SortKey or GlobalSecondaryIndexAWS SAM 在添加 SortKey 或 GlobalSecondaryIndex 时重新创建 DynamoDB 表
【发布时间】:2021-04-11 17:09:44
【问题描述】:

我正在使用AWS SAM 部署一个 DynamoDB 表,我的 template.yaml 看起来像这样:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

  DynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: owner
          AttributeType: S
      KeySchema:
        - AttributeName: owner
          KeyType: HASH

我使用sam build && sam deploy 来(重新)部署它。 当我添加 sortKey 和/或 GlobalSecondaryIndex 时,yaml 文件看起来像这样:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

  DynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: owner
          AttributeType: S
        - AttributeName: Timestamp
          AttributeType: S
      KeySchema:
        - AttributeName: owner
          KeyType: HASH
        - AttributeName: Timestamp
          KeyType: RANGE
      GlobalSecondaryIndexes:
        - IndexName: TestIndex
          KeySchema:
            - AttributeName: owner
              KeyType: HASH
            - AttributeName: Timestamp
              KeyType: RANGE
          Projection:
            ProjectionType: KEYS_ONLY

根据docs 更新这些字段应该是可能的(没有中断)。 但在我的情况下,部署命令总是重新创建整个表(删除所有数据)。 我在这里做错了吗?

编辑

也许我的解释不清楚。我尝试同时添加(GSI 和 sortKey),但我也尝试逐一添加,只需添加 GSI。

【问题讨论】:

    标签: amazon-dynamodb aws-sam


    【解决方案1】:

    DynamoDb 表的 key schema 和 LSI 只能在创建表时设置,以后只能添加 GSI。

    只是为了添加它,我们必须在 Sam/CloudFormation 中将名称属性添加到数据库、Dynamo 表等资源中,以避免被删除。当资源需要替换时,部署将失败,而不是删除并用新资源替换它。 例如:

    DynamoDBTable:
       Type: AWS::DynamoDB::Table
       Properties:
         TableName: "test-table"
         BillingMode: PAY_PER_REQUEST
    

    【讨论】:

    • TableName 是关键。谢谢
    【解决方案2】:

    添加排序键或以其他方式更改 KeySchema 需要替换表。请参阅正确的docs 以了解表定义。

    添加/更改 LSI 也需要更换。 无需中断即可添加 GSI。

    虽然我认为更改 GSI KeySchema 也需要替换 GSI...文档似乎暗示它不需要。

    【讨论】:

    • 如上所述。如果我只添加 GSI(不接触 KeySchema),它将尝试重新创建表。
    猜你喜欢
    • 2022-08-22
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 2018-10-10
    相关资源
    最近更新 更多