paint-brush
Using a NestJS Application with DynamoDB and Serverless Framework on AWSby@airscholar
7,288 reads
7,288 reads

Using a NestJS Application with DynamoDB and Serverless Framework on AWS

by Yusuf GaniyuJuly 21st, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

NestJS is a progressive Node.** js framework that helps build server-side applications. It is built with and fully supports TypeScript, yet still enables developers to code in pure JavaScript. The Serverless Framework is a free and open-source web framework written using Node.js. The first framework developed for building applications on AWS Lambda, a serverless computing platform provided by Amazon as a part of Amazon Web Services. In this article, I take you on a journey to fully setup your NestJS Application with a fully functional DynamoDB and Serverless.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Using a NestJS Application with DynamoDB and Serverless Framework on AWS
Yusuf Ganiyu HackerNoon profile picture


NestJS is a progressive Node. js framework that helps build server-side applications. It is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).You can read more on it here https://docs.nestjs.com.


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 Wikipedia or 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 here)


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)

NestJS (full documentation)

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.

Serverless Framework and DynamoDB

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 serverless.yml


https://gist.github.com/airscholar/838af29d63826790dea9407af406a2f2


Run the following commands to sync the plugins in your serverless.yml file

$ 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


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:

serverless dynamodb start --migrate error

If you have this kind of error being thrown while running serverless dynamodb start --migrate. The solution below is…

stackoverflow.com


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.


https://gist.github.com/airscholar/836b361691a70b0cc00068e3b04eb225#file-serverless-ts


You also need to create a .env file with the following keys. (NOTE: Port 6000 is the port specified in the serverless.yml file.)

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


In the src folder, create a subfolder db with db.service.ts and db.module.ts


https://gist.github.com/airscholar/72cea60ed77b23a043d65684c236c7ae#file-db-service-ts



Let’s create a todo module

HINT: Use this shortcut nest g resource todos --no-spec select REST APIpress enter and type y to generate CRUD endpoints.


Your updated file and folder structures should look like this.


https://gist.github.com/airscholar/b42b6b903febb249a2ba3602f6d6bf57#file-create-todo-dto-ts


https://gist.github.com/airscholar/641e9b4401936faaf7e78c9525a6e17c#file-update-todo-dto-ts


https://gist.github.com/airscholar/7edcd07789511c963d487afccbdc6a85#file-todo-entity-ts


https://gist.github.com/airscholar/d0560877f524ea82565a18e79e2403ca#file-todos-controller-ts


https://gist.github.com/airscholar/3495155722ca9dcbc8a7d83aebb12e95#file-todos-module-ts


https://gist.github.com/airscholar/6417bce64ea411b77fe6d0bce89c5029#file-todos-service-ts


https://gist.github.com/airscholar/4e50d544ef661ff1bf3b20ec4a79a63a#file-app-module-ts


That's it!


Run the app and sip your coffee!


yarn build && serverless offline start


Your app will be started and you should see something like this!



Testing Your app

Creating a Todo

Creating a Todo


Retrieving Todos


Updating todos


Retrieving updated todos


Deleting todos


Yes! You made it!

Link to the full source code

Thank you for reading.


Also Published Here