lop*_*zdp 7 yaml aws-cloudformation amazon-dynamodb infrastructure-as-code
我正在使用基础设施即代码在 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
Run Code Online (Sandbox Code Playgroud)
我不需要原始表中的键,我想要的只是为新 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
Run Code Online (Sandbox Code Playgroud)
但是,我得到的错误如下:
An error occurred: CaseRecords - Property Projection cannot be empty..
当我包括我的投影时,只有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
Run Code Online (Sandbox Code Playgroud)
但是,这也会返回错误:
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
Run Code Online (Sandbox Code Playgroud)
更新 #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
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它只与NonKeyAttributes
我在AttributeDefinitions
??? 中声明的一起工作?
Ped*_*tes 11
您需要table-name
在AttributeDefinitions
属性中添加。从文档:
属性定义
描述表和索引的键模式的属性列表。允许重复。
因此,即使您不使用原始表中的某些属性,您也必须声明能够在您的 GSI 中使用。
更新 #2 - 失败
您正在使用 keys 属性userId
,count
并且您在 at 处定义AttributeDefinitions
为NonKeyAttributes
(但它们是键属性)Projection
。您不需要添加它们,因为它们是自动投影的。从文档:
AWS::DynamoDB::表格投影
表示从表复制(投影)到索引中的属性。这些是对自动投影的主键属性和索引键属性的补充。
最终模板
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
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:
- count
ProjectionType: INCLUDE
Run Code Online (Sandbox Code Playgroud)
注意事项:
count
不应该打开,AttributeDefinitions
因为您没有将其用作密钥。
您不需要添加userId
atProjection
因为它会自动成为项目,因为它是在AttributeDefinitions
.
归档时间: |
|
查看次数: |
7037 次 |
最近记录: |