使用 Cloudformation 创建具有复合主键的 DynamoDB
Using Cloudformation to Create DynamoDB with composite primary key
通过命令行或在线 API,我可以轻松创建 "composite primary key",但是当我尝试使用 CloudFormation 为我完成这项工作时,我没有看到任何 JSON/YAML 让我设置一个叫做 "composite primary key" 的东西。语言完全不同,所以我希望有人能指导我如何使用 Cloudformation 创建这样的密钥。
我最好的猜测是如下所示,我希望复合键同时包含 userId 和 noteId:
Resources:
usersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: notes_serverless
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: noteId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: noteId
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
这是使用分区键和排序键创建 DynamoDB table 的 YAML 语法。
OP 上的语法几乎是正确的。我刚刚用正确的引号格式化并重新排列了属性的顺序。
AWSTemplateFormatVersion: "2010-09-09"
Resources:
usersTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
-
AttributeName: "userId"
AttributeType: "S"
-
AttributeName: "noteId"
AttributeType: "S"
KeySchema:
-
AttributeName: "userId"
KeyType: "HASH"
-
AttributeName: "noteId"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
TableName: "notes_serverless"
通过命令行或在线 API,我可以轻松创建 "composite primary key",但是当我尝试使用 CloudFormation 为我完成这项工作时,我没有看到任何 JSON/YAML 让我设置一个叫做 "composite primary key" 的东西。语言完全不同,所以我希望有人能指导我如何使用 Cloudformation 创建这样的密钥。
我最好的猜测是如下所示,我希望复合键同时包含 userId 和 noteId:
Resources:
usersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: notes_serverless
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: noteId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: noteId
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
这是使用分区键和排序键创建 DynamoDB table 的 YAML 语法。
OP 上的语法几乎是正确的。我刚刚用正确的引号格式化并重新排列了属性的顺序。
AWSTemplateFormatVersion: "2010-09-09"
Resources:
usersTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
-
AttributeName: "userId"
AttributeType: "S"
-
AttributeName: "noteId"
AttributeType: "S"
KeySchema:
-
AttributeName: "userId"
KeyType: "HASH"
-
AttributeName: "noteId"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
TableName: "notes_serverless"