问题描述
我正在使用 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。
解决方法
DynamoDb 表的 key schema 和 LSI 只能在创建表时设置,以后只能添加 GSI。
只是为了添加它,我们必须在 Sam/CloudFormation 中将名称属性添加到数据库、Dynamo 表等资源中,以避免被删除。当一个资源需要替换时,部署将失败,而不是删除它并用新资源替换它。 例如:
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: "test-table"
BillingMode: PAY_PER_REQUEST
,
添加排序键或以其他方式更改 KeySchema 需要替换表。请参阅表定义的正确 docs。
添加/更改 LSI 也需要更换。 可以不间断地添加 GSI。
虽然我认为更改 GSI KeySchema 也需要更换 GSI...文档似乎暗示它没有。