amazon-web-services - 带排序键的无服务器框架Dynamo DB表资源定义

标签 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

我的目标是使用主键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

最佳答案

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

An array of attributes that describe the key schema for the table and indexes.



请按以下方式编辑您的代码
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

有关更多CreateTable

关于amazon-web-services - 带排序键的无服务器框架Dynamo DB表资源定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47385177/

相关文章:

ios - 使用 alamofire upload 将图像上传到 AWS S3

amazon-web-services - 用于在 DynamoDB 分区键中存储 UUID 的二进制与字符串与数字?

node.js - 使用 Serverless 框架通过 VPC 设置 NAT 网关

python - 如何使用AWS Lambda部署大型Python包?

aws-lambda - 如何将参数传递给无服务器调用本地

amazon-web-services - 将 AWS CloudFront 连接到单点登录 (SSO)

sql-server - AWS 上单个 m1.large 实例的 Sql Server 企业版许可费用是多少?

ruby - Amazon SNS 静默推送

amazon-web-services - 如何将 IoT 流数据映射到索引 Dynamo DB 列

java - 我如何连续监听 Android 应用程序中的 Dynamo DB 更改?