使用CloudFormation创建具有复合主键的DynamoDB

9

无论是从命令行还是在线API中,我都可以轻松创建“复合主键”,但是当我尝试使用CloudFormation来完成这项工作时,我没有看到任何JSON / YAML可以让我设置所谓的“复合主键”。语言完全不同,因此我希望有人能指导我如何使用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

你能展示一下你用来创建具有复合主键的表的命令吗? - notionquest
我只需要正确的YAML语法。实际上,我正在使用serverless框架来部署云形成代码。在Web API上,有一个简单的框可以勾选。 - user985030
1个回答

22

这里是使用分区键和排序键创建DynamoDB表的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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接