DEV Community

Juan P. Lima
Juan P. Lima

Posted on

Deploying your NestJS API on AWS Lambda

If it's your first time dealing with AWS Lambda or if you would like to know a little more about it, you can check out my first article on the AWS Lambda series that will explain to you what AWS Lambda is, how to use, when to use, and when not to; you can find it right here -> Click Me <-

Starting your NestJS Project

In this section, I'll start a new NestJS project, if you already have one, feel free to skip this section.
First, we'll need to globally install Nest's cli: npm i -g @nestjs/cli , then you can generate a new NestJS project using nest new your-project-name, after doing so, your project will be generated, your folder should look something like this:
My project's folder after running nest cli command

Setting up serverless framework

Now that you have your NestJS project, let's set up the serverless framework that will help us deploy our API to AWS Lambda, first, you'll need to install the following packages:
npm i @vendia/serverless-express
npm i -g serverless
Once it's done, we should create our handler function to handle Lambda's events, I'll create mine on the file lambda.ts I've created on the "src" folder, there we'll start our NestJS-Express server and create a function that will deal with our events:
Lambda.ts file

Finally, we should create the serverless.yml file that will configure the serverless framework, you should create it on the root directory, there you'll be able to set the function's timeout, memory, region…
Serverless.yml file

Deploying

Before deploying your project to AWS Lambda, you'll need to configure your credentials on serverless, I won't be covering it here, but you can check Serverless' documentation on that here -> Click Me <-

Now that it's all set up, you can run serverless deploy , wait a few seconds and your project will have been deployed, you can check it on AWS Lambda's page:
AWS Lambda function list page

You can click on your function to see more details and test if it's working as expected, you can find the link to do it on configuration->triggers:
My Lambda's function page

With that link, you can use your favorite API Client to test if it's working as expected:
API Client testing my Lambda function

As you can see, mine is working just fine.

Conclusions

As you guys can see, its really simple to deploy your NestJS project to AWS Lambda, this was the third article of my AWS Lambda series, you can check the previous articles on the following links:
-> AWS Lambda: What it is, how to use, when to use, and when not to<-
-> Deploying a NodeJS - Express API to AWS Lambda <-

Github repository with my example -> Click Me <-

NestJS documentation about serverless framework -> Click Me <-

Top comments (0)