代码之家  ›  专栏  ›  技术社区  ›  user504909

dynamodb如何在serverless.yml中定义无键模式?

  •  1
  • user504909  · 技术社区  · 6 年前

    我尝试在我的无服务器aws lambda中应用dynamodb。 我的文件如下:

    resources:
      Resources:
        StoreDynamoDbTable:
          Type: 'AWS::DynamoDB::Table'
          DeletionPolicy: Retain
          Properties:
            AttributeDefinitions:
              - AttributeName: id
                AttributeType: S
              - AttributeName: lat
                AttributeType: N 
              - AttributeName: lng
                AttributeType: N
            KeySchema:
              - AttributeName: id
                KeyType: HASH
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
            TableName: ${self:provider.environment.TableStore}
    

    我尝试应用lat和lng作为storeTable的属性,只是属性不是key Schema,但是每个store元素都应该有这些属性。

    出现错误:StoreDynamoDbTable-属性AttributeDefinitions 与表和次表的KeySchema不一致

    1 回复  |  直到 6 年前
        1
  •  5
  •   Quentin Hayot    6 年前

    See AWS docs )

    如果 id

    resources:
      Resources:
        StoreDynamoDbTable:
          Type: 'AWS::DynamoDB::Table'
          DeletionPolicy: Retain
          Properties:
            AttributeDefinitions:
              - AttributeName: id
                AttributeType: S
            KeySchema:
              - AttributeName: id
                KeyType: HASH
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
            TableName: ${self:provider.environment.TableStore}
    


    此外,如果希望在密钥架构中使用日期作为排序密钥,可以使用以下内容:

    resources:
      Resources:
        StoreDynamoDbTable:
          Type: 'AWS::DynamoDB::Table'
          DeletionPolicy: Retain
          Properties:
            AttributeDefinitions:
              - AttributeName: id
                AttributeType: S
              - AttributeName: date
                AttributeType: S
            KeySchema:
              - AttributeName: id
                KeyType: HASH
              - AttributeName: date
                KeyType: RANGE
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
            TableName: ${self:provider.environment.TableStore}
    

    HASH )键,并且可以选择进行排序( RANGE )钥匙。 Check this to learn more about DynamoDB's key schema.