问题描述
我正在学习 DynamoDB。
我想用以下主键创建一个表:
- 分区键:名称
- 排序键:creationDate
并具有以下属性:
- 姓名 (pk)
- 地址(sk)
- 创建日期
- isActive
这是我的 TS 界面(以防万一它有助于描述我想要的)
interface User {
readonly name: string;
readonly address: string;
readonly creationDate: Date;
readonly isActive: boolean;
}
我编写了以下模板:
Resources:
myDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDeFinitions:
- AttributeName: 'name'
AttributeType: 'S'
- AttributeName: 'address'
AttributeType: 'S'
- AttributeName: 'creationDate'
AttributeType: 'S'
- AttributeName: 'isActive'
AttributeType: 'S'
KeySchema:
- AttributeName: 'name'
KeyType: HASH
- AttributeName: 'creationDate'
KeyType: RANGE
One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDeFinitions
我用谷歌搜索并找到了几个答案,但我仍然不明白发生了什么以及如何在我的表中包含这些属性(所有项目都将具有这些属性)。
有什么办法可以做到吗?如果是这样,我做错了什么? 谢谢
解决方法
您的 isActive
中不能有 address
和 AttributeDefinitions
,因为它们不在您的 KeySchema
中。您还缺少 ProvisionedThroughput
。所以正确的表定义是(示例):
Resources:
myDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: 'name'
AttributeType: 'S'
- AttributeName: 'creationDate'
AttributeType: 'S'
KeySchema:
- AttributeName: 'name'
KeyType: HASH
- AttributeName: 'creationDate'
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1