Infrastructure Automation for DynamoDB
This is part 3 of the production-ready Serverless series.
In the last blog, I talked about the hows and whys of Infrastructure as Code. In this blog post, we will discuss how to automate the creation of a DynamoDB table for the ToDo application which we are creating.
Quick Recap on Architecture
Todo app is a CRUD-based application that contains DynamoDB as the backend.
If you are new to DynamoDB, to get started you can use the following blog post.
We are now going to start creating our infrastructure in SST using AWS CDK, starting with DynamoDB.
Create a Stack
To recap quickly, we started the project using the SST template and more details can be found in the starting post in the series.
https://aws.plainenglish.io/getting-started-with-serverless-stack-fc871eba5357
Add the following to a new file in stacks/StorageStack.js
Let's quickly go over the code here, we have created a new class called StorageStack which will be used to create both DynamoDB and S3.
Here I am using the SST stack to create a DynamoDB with the fields such as:
- userId: The Id of the user to whom the ToDo belongs to.
- todoId: Id of ToDo.
DynamoDB is a key-value document database and designing a key plays a major role and in this case, I have designed the key, so I can query the table by the user as well as ToDos.
Another interesting point about the code is I am exposing the table property:
table; // Public property which exposes Dynamodb for the other stacks like Lambda functions to access.
So the table name can be used for future access in the API projects.
Now let's add the app to the Index.js:
Deploy the App
Now switch over to your terminal and type SST start
and this will deploy the app with the table in the AWS console.
In the next blog post, we will create an API Gateway and Lambda functions for the CRUD operation.
More content at plainenglish.io