DynamoDb is a brilliant NoSQL database from AWS. It integrates directly with the Serverless Framework, so today we're going to configure Serverless to work with DynamoDb.
First of all, let's add a stage of "dev" and a region of "us-east-1", followed by our Serverless profile name to our Serverless.yml file.
Next let's add an environment variable to hold our DynamoDb table name, which is designed in a way to be based on the service name, the current deployment stage, and then the table name itself. This allows us to ensure we create separate DynamoDb tables per deployment environment.
The next step is to define our IAM Role statements, which define the actions that will be available to our Lambda functions. Finally let's restrict our IAM role permissions to this specific table (for the current stage that we are deploying to.)
Next we'll create a resource to generate our DynamoDb table. Things to note here are that we have added a DeletionPolicy of Retain, to prevent us from accidentally deleting our Serverless DynamoDb table resources. Under the properties section you will also see that we've explicitly stated that we want to use our environment variable for the table name, as previously defined. Below this we've defined our primary key, and finally our BillingMode to be PAY_PER_REQUEST, since we are just starting out with our DynamoDb implementation.
Finally let's run SLS Deploy, and head to AWS Management Console to review our deployment.
As you can see, Serverless has created our DynamoDb table based on our Serverless.yml configuration. Enjoy!
Download Github Code Sample
Feel free to DOWNLOAD the sample code of the aforementioned implementation from my Github account.