Can a single serverless.yml provide access to multiple dynamodb resources?

Hi, quick question … One of the suggestions I’ve seen to ensure that dynamodb scales properly is to design primary and sort keys based upon page needs, such that a page does not have to join query results. My understanding is that in some cases this may mean writing the same information to two separate dynamodb tables, with each table having different primary/sort keys. All of the examples I’ve seen for serverless.yml files so far utilize just one dynamodb resource, and the list of available properties at https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/ seems to suggest that there is no explicit way to link a function to one of several defined resources. If I am understanding the situation properly, the serverless.yml defines at most a single dynamodb resource, which the functions use. But, it should be pretty common for people to need access to multiple dynamodb resources in one lambda function. How would a person go about implementing this? Is there a pre-existing example somewhere? Or, is this so far impossible?

You can define as many DynamoDB’s in your serverless.yml as you want. Just add them to the resources and iamRoleStatements sections. Your Lambda will then be able to access all of them. My two tips would be:

  1. Make sure you include the stage name in the table name. This will save problems later when you try to deploy different stages to the same AWS account.
  2. Pass the table name into the Lambda using environment variables. This makes it easy to give the same table different names for each stage.
1 Like