【问题标题】:Using Cloudformation to Create DynamoDB with composite primary key使用 Cloudformation 创建具有复合主键的 DynamoDB
【发布时间】:2017-12-13 17:32:33
【问题描述】:

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

【问题讨论】:

  • 你能显示你用来创建具有复合主键的表的命令吗?
  • 我只需要正确的 YAML 语法。我实际上是使用无服务器框架来部署云形成代码。在 Web API 上有一个简单的复选框供您选中。

标签: amazon-dynamodb amazon-cloudformation


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 2022-09-28
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多