NestJS is a progressive Node. js framework that helps build server-side applications. It is built with and fully supports
A lot of people are having challenges setting up Serverless Framework with DynamoDB, if you happen to fall within this category, then this is the article you have been looking for! Sit tight, relax and grab a cup of coffee while I take you on a journey to fully setup your NestJS Application with a fully functional DynamoDB and Serverless Framework :).
A little bit of 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 to and derives its name from Dynamo, but has a different underlying implementation. (Read more on DynamoDB on
Wikipediaor visit the docs)
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 for building applications on AWS Lambda, a serverless computing platform provided by Amazon as a part of Amazon Web Services. (Full details
In order to get started you on this journey, you will need to get the following installed on your machine (you can ignore this section if you have the installations done on your machine)
To install NestJS on your machine, run the following commands on your terminal to get you started. You can give your project any name you want but in this article, I will be naming it nest-serverless-dynamo.
$ npm i -g @nestjs/cli $ nest new nest-serverless-dynamo
Use the arrow key to select your desired package manager, in my case,yarn.
Now that NestJS CLI has been installed and a new project created, open up the project in your favorite IDE (mine is VsCode 😀) and let’s fire on to the juicy part.
To get started with the 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 done, you need to create a new file in the root directory
Run the following commands to sync the plugins in your
$ serverless plugin install -n serverless-plugin-optimize $ serverless plugin install -n serverless-dynamodb-local $ serverless plugin install -n serverless-offline
Run this command in your root directory (the same folder where your
$ serverless dynamodb install
NOTE: At this point, your folder structure should look like this. If not, you must have skipped a step, retrace your steps before continuing!
Start your DynamoDB locally to test if you are having this error, if not, GREAT! If you are, check the solution below:
If you made it to this point, you are now a serverless guru! 😄. Do not get tired, we are 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
IS_OFFLINE = 'true' DYNAMODB_ENDPOINT = 'http://localhost:6000/shell'
We are pretty much done with the setups, let’s now focus on the app itself.
The only part left is to connect our NestJS app to Dynamo DB
src folder, create a subfolder
Let’s create a todo module
HINT: Use this shortcut
nest g resource todos --no-specselect
REST APIpress enter and type
yto generate CRUD endpoints.
Your updated file and folder structures should look like this.
Run the app and sip your coffee!
yarn build && serverless offline start
Your app will be started and you should see something like this!
Creating a Todo
Yes! You made it!
Thank you for reading.
Also Published Here