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

时间:2017-07-10 05:25:12

标签: amazon-dynamodb amazon-cloudformation

通过命令行或在线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

1 个答案:

答案 0 :(得分:7)

以下是使用分区和排序键创建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"