Using a NestJS Application with DynamoDB and Serverless Framework on AWS
NestJS is a progressive knot. js framework that helps to build server-side applications. It is built with and fully supports
Many people have difficulty setting up Serverless Framework with DynamoDB. If you belong to this category, this article is what you were looking for! Sit back, relax, and have a cup of coffee as I take you on a journey to fully configure your NestJS application with a fully functional DynamoDB and serverless framework :).
A little housekeeping…
Amazon DynamoDB is a fully managed, proprietary NoSQL database service that supports key-value and document data structures and is offered by Amazon.com as part of the Amazon Web Services portfolio. DynamoDB exposes a similar data model and takes its name from Dynamo, but has a different underlying implementation. (Learn more about DynamoDB at
Wikipedia or visit thedocuments )
And of course, Serverless Framework…
The Serverless Framework is a free and open source web framework written using Node.js. Serverless is the first framework developed to build applications on AWS Lambda, a serverless computing platform provided by Amazon as part of Amazon Web Services. (All the details
here )
In order to embark on this journey, you will need to install the following on your machine (you can skip this section if you have done the installs on your machine)
To install NestJS on your machine, run the following commands on your terminal to get started. You can give your project any name you want, but in this article I will name it nest-serverless-dynamo.
$ npm i -g @nestjs/cli
$ nest new nest-serverless-dynamo
Use the arrow key to select the desired package manager, in my case,thread.
Now that NestJS CLI has been installed and a new project created, open the project in your favorite IDE (mine is VsCode 😀) and let’s get to the juicy part.
To get started with Serverless Framework, you need to run the following commands.
$ yarn add aws-lambda aws-serverless-express express aws-sdk
$ yarn add @serverless/utils
Once the installation is complete, you need to create a new file in the root directory serverless.yml
Run the following commands to sync the plugins in your serverless.yml
case
$ serverless plugin install -n serverless-plugin-optimize
$ serverless plugin install -n serverless-dynamodb-local
$ serverless plugin install -n serverless-offline
To install DynamoDB
Run this command in your root directory (the same folder where your serverless.yml
is)
$ serverless dynamodb install
REMARK: At this point, your folder structure should look like this. Otherwise, you must have skipped a step, retrace your steps before continuing!
Start your DynamoDB locally to test if you encounter this error, otherwise GREAT! If so, see the solution below:
If you encounter this kind of error when running dynamodb start –migrate without a server. The solution below is…
stackoverflow.com
If you’ve gotten to this point, you’re now a serverless guru! 😄. Don’t get tired, we’re almost done!
We are almost there. Navigate to the src folder and create a serverless.ts
file with the content below.
You also need to create a .env
file with the following keys. (NOTE: Port 6000 is the port specified in the serverless.yml
case.)
IS_OFFLINE = 'true'
DYNAMODB_ENDPOINT = 'http://localhost:6000/shell'
We’re pretty much done with the configurations, now let’s focus on the app itself.
All that remains is to connect our NestJS application to Dynamo DB
In the src
folder, create a subfolder db
with db.service.ts
and db.module.ts
Let’s create a todo module
TIP: use this shortcut
nest g resource todos --no-spec
selectREST API
press enter and typey
to generate CRUD endpoints.
Your updated file and folder structures should look like this.
That’s it!
Run the app and sip your coffee!
yarn build && serverless offline start
Your application will start and you should see something like this!
Test your app
Create a task
Yes! You did it!
Thanks for the reading.
Also posted here
LOADING
. . . comments & After!
Comments are closed.