我正在使用基础架构即代码在 CloudFormation 中实施 GSI。我想要做的就是使用这个表来记录主 DynamoTable 中的条目数。这是主要故事的样子:
Resources:
CaseRecords:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.tableName}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: caseRecordId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: caseRecordId
KeyType: RANGE
我不需要原始表中的键,我想要的只是为新 GSI 创建一个新的 HASH 键,它会告诉我我正在跟踪的计数来自哪个表,即上面的表。
以下是我迄今为止尝试实施 GSI 的方式:
# Implement a GSI to handle item count totals
GlobalSecondaryIndexes:
- IndexName: gsiCaseCountTable
KeySchema:
- AttributeName: table-name
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
但是,我得到的错误如下:
An error occurred: CaseRecords - Property Projection cannot be empty..
当我包含我曾经的 PROJECTION 时,仅userId
来自原始表的只是为了跟踪每个用户在原始表中的条目计数,我尝试以下操作:
Implement a GSI to handle item count totals
GlobalSecondaryIndexes:
- IndexName: gsiCaseCountTable
KeySchema:
- AttributeName: table-name
KeyType: HASH
Projection:
NonKeyAttributes:
- userId
ProjectionType: INCLUDE
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
但是,这也会返回错误:
An error occurred: CaseRecords - Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes.
如何使用 CloudFormation 模板在 Dynamo 中正确实现全局二级索引,以便在原始表中记录条目数????
谢谢。
更新
如果有人想知道这就是我能够部署它的方式。这不是一个完美的解决方案,但它让我可以跟踪并计算项目表中的条目:
# NOTE: DynamoDB Serverless Configuration
# NoSQL Table for CaseRecord DB
Resources:
CaseRecords:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.tableName}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: caseRecordId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: caseRecordId
KeyType: RANGE
# Set the capacity based on the stage
# ProvisionedThroughput:
# ReadCapacityUnits: ${self:custom.tableThroughput}
# WriteCapacityUnits: ${self:custom.tableThroughput}
# Implement a GSI to handle item count totals
GlobalSecondaryIndexes:
- IndexName: gsiCaseCountTable
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: KEYS_ONLY
更新 #2 - 失败
根据下面@Pedro Arantes 提供的信息,我正在尝试使用我想要使用的属性定义来实现 GSI。然而,这也失败了。下面是实施,这是我使用的 AWS Doc 的链接:AWS GSI Doc,这是失败的实施:
Resources:
CaseRecords:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.tableName}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: caseRecordId
AttributeType: S
- AttributeName: table-name
AttributeType: S
- AttributeName: count
AttributeType: N
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: caseRecordId
KeyType: RANGE
# Set the capacity based on the stage
# ProvisionedThroughput:
# ReadCapacityUnits: ${self:custom.tableThroughput}
# WriteCapacityUnits: ${self:custom.tableThroughput}
# Implement a GSI to handle item count totals
GlobalSecondaryIndexes:
- IndexName: gsiCaseCountTable
KeySchema:
- AttributeName: table-name
KeyType: HASH
Projection:
NonKeyAttributes:
- userId
- count
ProjectionType: INCLUDE
我怎样才能让它只与NonKeyAttributes
我在AttributeDefinitions
???