带有排序键的无服务器框架Dynamo DB表资源定义

Jos*_*han 6 amazon-web-services amazon-dynamodb serverless-framework

我目前在我的serverless.yml中有一些代码.

resources:
  Resources:
    uploadBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:service}-${self:custom.stage}-uploads
    visitsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.visitsTable}
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
          - AttributeName: visitId
            AttributeType: S
          - AttributeName: comments
            AttributeType: S
          - AttributeName: attachments
            AttributeType: S
          - AttributeName: ph
            AttributeType: N
          - AttributeName: ch
            AttributeType: N
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
          - AttributeName: visitId
            KeyType: HASH
        ProvisionedThroughput:
            ReadCapacityUnits: 5
            WriteCapacityUnits: 5
Run Code Online (Sandbox Code Playgroud)

我的目标是创建一个包含主键userId,排序键visitId的表,并包含注释,附件,ph&ch的字段.当我尝试时,sls deploy我得到以下错误.

无服务器错误---------------------------------------

发生错误:visitsTable - 属性AttributeDefinitions与表的KeySchema和二级索引不一致.

我在这做错了什么?

编辑:我试过的另一次尝试

resources:
  Resources:
    uploadBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:service}-${self:custom.stage}-uploads
    visitsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.visitsTable}
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
          - AttributeName: visitId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
          - AttributeName: visitId
            KeyType: RANGE
        ProvisionedThroughput:
            ReadCapacityUnits: 5
            WriteCapacityUnits: 5
Run Code Online (Sandbox Code Playgroud)

Vai*_* PS 11

AWS DynamoDb是一个NO-SQL类型的数据库,无需在创建表时定义所有键.同样从AWS文档中可以清楚地看到,在Attribute Definition中,您必须指定Key模式和索引.

描述表和索引的键架构的属性数组.

请编辑您的代码,如下所示

resources:
  Resources:
    uploadBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:service}-${self:custom.stage}-uploads
    visitsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.visitsTable}
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
          - AttributeName: visitId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
          - AttributeName: visitId
            KeyType: RANGE
        ProvisionedThroughput:
            ReadCapacityUnits: 5
            WriteCapacityUnits: 5
Run Code Online (Sandbox Code Playgroud)

更多CreateTable