Multiple DynamoDB tables to one service

Hi there,

I’am trying to expand the notes api with some more tables.
Currently I have added a few more dynamodb-tables-XYZ.yml files and referenced them at the bottom in my serverless.yml file (under ressources).

But how do I implement multiple “custom” blocks in one serverless.yml file (at the top of the file) where it describes the naming convention used for specific environments, more specific:

custom:
  # Our stage is based on what is passed in when running serverless
  # commands. Or fallsback to what we have set in the provider section.
  stage: ${opt:stage, self:provider.stage}
  # Set the table name here so we can use it while testing locally
  tableName: ${self:custom.stage}-notes
  # Set our DynamoDB throughput for prod and all other non-prod stages.
  tableThroughputs:
    prod: 5
    default: 1
  tableThroughput: ${self:custom.tableThroughputs.${self:custom.stage}, 
self:custom.tableThroughputs.default}
  # Load our webpack config
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true
  # Load our secret environment variables based on the current stage.
  # Fallback to default if it is not in prod.
  environment: ${file(env.yml):${self:custom.stage}, file(env.yml):default}

Kind regards
-Tony

Maybe the answer relies in this part of the book? https://serverless-stack.com/chapters/organizing-serverless-projects.html if I follows theese steps I might be able to ad more tables and api’s and tie them together?

Kind regards

  • Tony

Hmm I’m not sure what you mean by multiple custom blocks. You can keep adding that block up top. Can you give me an example of what you are looking to do?

Hi,
By multiple custom blocks i mean a way to add more tables in the serverless.yml file. Right now there is one custom block that contains the definition for one table. But if I have more than one table, how woul my serverless.yml file look like, more precisely the custom block?
Right now I’am following this chapter https://serverless-stack.com/chapters/organizing-serverless-projects.html so maybe it will fix the problem for me (at least I think so for now).

I don’t think there is any restriction in the number of things you can define in a custom block. So you can use different variable names for the different tables and they should work fine.

You might want to read through the AWS guide for Serverless to get a better understanding of the serverless.yml file https://serverless.com/framework/docs/providers/aws/guide/. I was still a bit in the dark after finishing the tutorial, until I reviewed it.

If I understand your question, though, to add another table, you need to create another table declaration in one of the Resources sections, so either in the main serverless.yml file, or in one of the sub files (likely dynamodb-table.yml).

Not to go too far off on a tangent, but you might want to look into NoSQL table design best practices, as you can avoid adding tables through well-planned key design. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices.html

1 Like

You should really take some time to check out this video.

You want only one table per service. Stop thinking the same was as if your were implementing relational tables or your services will cost your exponentially more money.

There are very very few use cases that require more than one table.

The important thing to do is to design your SORT keys properly to minimize the READS (RCU) to your NoSQL DB (Dynamo).

From there additional views should be handled with GSI.

Use LSI’s (Local Secondary Indexes) to give you additional ways to sort your views. Please watch this:

1 Like

Yup that’s a great video on how to structure your DynamoDB tables!

im talking crap like I know what im doing and im literally struggling with implementing a query to a GSI right now… lol

What would be a good way to implement a GSI to then write to said GSI? any ideas?

Thanks!

Hmm I’m not familiar with a GSI. What is it?