Norak
Norak

Reputation: 413

Creating two dynamoDB tables in serverless.yml

I'm try to add 2 tables to serverless.yml to link with DynamoDB.

A part of my code in 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

Is that correct??

My tables are:

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

Do I need more changes in serverles.yml? what is the meaning of the expressions "ReadCapacityUnits" and "WriteCapacityUnits"?

Upvotes: 2

Views: 11200

Answers (2)

ArtiomLK
ArtiomLK

Reputation: 2260

Short Answer:

Read and Write Capacity Units are the max size of data the db is allowed to processes per second, if you go over this amount in any second your request would throttle.

Alternative:

It might be easier to just use DynamoDB On-Demand and pay for the Db table usages rather than calculating WCU and RCU.

Example

Here is an example of 3 tables added in a formatted manner and without semiquotes:

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

Additional Explanation with Examples:

Back to Read/Write Capacity Mode:

Write Capacity Units (WCU) formula: Round up (DataSize / 1KB)

Example1: imagine you foresee a traffic of writing 10KB of data per second into the db. Using the WCU formula, you would need (10KB / 1KB) = 10WCU.

Example2: Expecting writing traffic of 7.5KB of data to the db, we would need: (7.5KB / 1KB) = 8WCU

Reading Capacity Units (RCU) depends on Strongly or Eventually Consistent models.

Strongly Consistent mode: Round up (DataSize / 4KB)
Eventually Consistent mode: Round up(DataSize / 4KB) / 2

Upvotes: 3

notionquest
notionquest

Reputation: 39216

There should be some separation between two resources (i.e. two DynamoDB tables).

Note:-

You can define only key attributes while creating the DynamoDB table. In other words, you don't need to define all other non-key attributes.

Try this:-

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    

Read and Write capacity units:-

You specify throughput capacity in terms of read capacity units and write capacity units:

One read capacity unit represents one strongly consistent read per second, or two eventually consistent reads per second, for an item up to 4 KB in size. If you need to read an item that is larger than 4 KB, DynamoDB will need to consume additional read capacity units. The total number of read capacity units required depends on the item size, and whether you want an eventually consistent or strongly consistent read. One write capacity unit represents one write per second for an item up to 1 KB in size. If you need to write an item that is larger than 1 KB, DynamoDB will need to consume additional write capacity units. The total number of write capacity units required depends on the item size.

Read and write capacity units

Upvotes: 7

Related Questions