Using a NestJS Application with DynamoDB and Serverless Framework on AWS by@airscholar

Using a NestJS Application with DynamoDB and Serverless Framework on AWS

July 21st 2022 1,119 reads
Read on Terminal Reader
Open TLDR
react to story with heart
react to story with light
react to story with boat
react to story with money
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.
image
Yusuf Ganiyu HackerNoon profile picture

Yusuf Ganiyu

Finding new solutions to old problems

github social icontwitter social iconlinkedin social icon


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.

image

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



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

image


NOTE: At this point, your folder structure should look like this. If not, you must have skipped a step, retrace your steps before continuing!


image

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.



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




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.


image

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


image

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



Testing Your app

Creating a Todo

Creating a Todo

Creating a Todo


Retrieving Todos

Retrieving Todos


Updating todos

Updating todos


Retrieving updated todos

Retrieving updated todos


Deleting todos

Deleting todos


Yes! You made it!

Link to the full source code

Thank you for reading.


Also Published Here


react to story with heart
react to story with light
react to story with boat
react to story with money

Related Stories

L O A D I N G
. . . comments & more!