Implementing NestJS API using AWS DynamoDB locally with Docker
DynamoDB has become one of the most used and reliable NoSQL databases due to its ability to scale, being serverless and most importantly Amazon backs it. However, setting up one for development purposes can be challenging given the number of configurations and permissions one has to enable from the AWS console, sometimes it can also be costly having it running on the AWS console.
In this article, we will try to run the DynamoDB locally using a docker image and connect it to a NestJS API.
DynamoDB Setup
Run the following command to pull the docker image:
docker pull amazon/dynamodb-local
Once pulled, execute this command to get the DynamoDB running:
docker run -p 8000:8000 amazon/dynamodb-local
You can check the available tables by executing this command:
aws dynamodb list-tables --endpoint-url http://localhost:8000
In order to access DynamoDB programmatically or through the AWS Command Line Interface (AWS CLI), you must configure your credentials to enable authorization for your applications.
aws configure
You can use a fake Access Key Id & Secret Access Key and set the region to local given this configuration is for development purposes.
Project Setup
Let’s generate a NestJS project using this command:
nest new project-name
The project-name
directory will be created, node modules and a few other boilerplate files will be installed, and a src/
directory will be created and populated with several core files.
Then create a file under the project directory called table-script.json
and add the following content:
Inside this script file, we specified book
as the table name and bookId
to be the primary key and set its data type to be a string.
Let’s create the table by executing the script file under the project directory:
aws dynamodb create-table --cli-input-json file://table-script.json --endpoint-url http://localhost:8000
Now the table is created, let’s install a couple of dependencies that will help us to implement the API and connect to the DB.
yarn add aws-sdk @aws-sdk/client-dynamodb class-validator uuid
Under the src/
directory, create a folder called aws-config
which will contain a file named dynamoDBClient.ts
and paste the following content:
The default endpoint URL is http://localhost:8000
and the region was previously set to local
.
Let us generate a NestJS resource called book under the src/
folder by running this command:
nest g resource book
Create the book.dto.ts
file under the book
directory and add the following code snippets:
Open the book.service.ts
file and paste the following content:
In the above code snippets, we pretty much implemented all the CRUD operations using the DynamoDB client SDK. You can read more on DynamoDB CRUD operations here.
Now the service is set, open the book.controller.ts
and update it with this content:
With everything in place, we can easily run the API server by executing this command:
yarn start:dv
From here, you can test all the endpoints using your preferred test tool such as Postman.
Conclusion
There are several ways to run DynamoDB locally but in this tutorial, we used the docker image approach in order to achieve that and also learned how we can programmatically connect to DynamoDB locally using the client SDK to our NestJS API.
The full codes of this article can be found here.