在serverless.yml

时间:2017-11-16 10:52:21

标签: node.js amazon-dynamodb serverless-framework

我尝试将2个表添加到serverless.yml以与DynamoDB链接。

我在serverless.yml中的代码的一部分:

...       
 resources:
      Resources:
        ItemsTable:
          Type: "AWS::DynamoDB::Table"
          Properties:
            TableName: "InvoiceConfig"
            AttributeDefinitions:
            - AttributeName: "providerName"
              AttributeType: "S"
            KeySchema:
            - AttributeName: "providerName"
              KeyType: "HASH"
            ProvisionedThroughput:
              ReadCapacityUnits: 2
              WriteCapacityUnits: 2
            TableName: "DifferentTermsPages"
            AttributeDefinitions:
            - AttributeName: "id"
              AttributeType: "S"
            - AttributeName: "providerName"
              AttributeType: "S"
            - AttributeName: "productType"
              AttributeType: "S"
            - AttributeName: "language"
              AttributeType: "S"
            - AttributeName: "terms"
              AttributeType: "L"
            KeySchema:
            - AttributeName: "id"
              KeyType: "HASH"
            - AttributeName: "providerName"
              KeyType: "HASH"
            - AttributeName: "productType"
              KeyType: "HASH"
            - AttributeName: "language"
              KeyType: "HASH"
            - AttributeName: "terms"
              KeyType: "HASH"
            ProvisionedThroughput:
              ReadCapacityUnits: 10
              WriteCapacityUnits: 10

这是正确的吗?

我的表是:

InvoiceConfig: with columns: providerName (String)
DifferentTermsPages: id (String), providerName (String), productType (String), language (String), terms (list)

我是否需要在serverles.yml中进行更多更改?表达式“ReadCapacityUnits”和“WriteCapacityUnits”是什么意思?

2 个答案:

答案 0 :(得分:2)

两个资源(即两个DynamoDB表)之间应该有一些分离。

注意: -

您可以在创建DynamoDB表时仅定义键属性。换句话说,您不需要定义所有其他非键属性。

试试这个: -

Resources:
ItemsTable:
  Type: "AWS::DynamoDB::Table"
  Properties:
    TableName: "InvoiceConfig"
    AttributeDefinitions:
    - AttributeName: "providerName"
      AttributeType: "S"
    KeySchema:
    - AttributeName: "providerName"
      KeyType: "HASH"
    ProvisionedThroughput:
      ReadCapacityUnits: 2
      WriteCapacityUnits: 2            
DifferentTermsPages:
  Type: "AWS::DynamoDB::Table"
  Properties:             
    TableName: "DifferentTermsPages"
    AttributeDefinitions:
    - AttributeName: "id"
      AttributeType: "S"
    KeySchema:
    - AttributeName: "id"
      KeyType: "HASH"
    ProvisionedThroughput:
      ReadCapacityUnits: 10
      WriteCapacityUnits: 10    

读写容量单位: -

  

您可以根据读取容量单位和指定吞吐量容量   写容量单位:

     

一个读取容量单位代表一个强烈一致的读取   对于一个项目,第二个或两个最终一致的读取每秒   到4 KB的大小。如果您需要读取大于4 KB的项目,   DynamoDB需要消耗额外的读取容量单位。该   所需的读取容量单位总数取决于项目大小,   以及您是否希望最终一致或强烈一致   读。一个写入容量单位表示每秒一次写入   项目大小不超过1 KB。如果你需要写一个更大的项目   如果超过1 KB,DynamoDB将需要消耗额外的写入容量   单位。所需的写入容量单位总数取决于   项目大小。

Read and write capacity units

答案 1 :(得分:1)

简短答案:

读和写容量单位是每秒允许数据库处理的最大数据大小,如果您在任何一秒钟内超过此数量,则请求将受到限制。

替代:

仅使用DynamoDB On-Demand并为Db表使用付费而不是计算WCU和RCU可能会更容易。

示例

以下是3个以格式化方式添加且没有半引号的表的示例:

resources:
  Resources:
    myDynamoDBTable1:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: Table1
        AttributeDefinitions:
          - AttributeName: ColumnName1
            AttributeType: S
          - AttributeName: ColumnName2
            AttributeType: N
        KeySchema:
          - AttributeName: ColumnName1
            KeyType: HASH
          - AttributeName: ColumnName2
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1 
    myDynamoDBTable2:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: Table2
        AttributeDefinitions:
          - AttributeName: ColumnName1
            AttributeType: S
        KeySchema:
          - AttributeName: ColumnName1
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST
    myDynamoDBTableN:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: TableN
        AttributeDefinitions:
          - AttributeName: ColumnName1
            AttributeType: S
        KeySchema:
          - AttributeName: ColumnName1
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST

示例附加说明:

返回Read/Write Capacity Mode

写入容量单位(WCU)公式:向上舍入(DataSize / 1KB)

示例1: 假设您预见到每秒将10KB数据写入数据库的流量。使用WCU公式,您需要(10KB / 1KB)= 10WCU。

示例2 : 预期将7.5KB数据写入数据库的流量,我们将需要:(7.5KB / 1KB)= 8WCU

读取容量单位(RCU)取决于强或最终一致的模型。

严格一致的模式:向上舍入(DataSize / 4KB)
最终一致的模式:向上舍入(DataSize / 4KB)/ 2