Deploying your NestJS API on AWS Lambda
Juan P. Lima
Posted on January 24, 2023
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:
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:
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…
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:
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:
With that link, you can use your favorite API Client to test if it's working as expected:
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 <-
Posted on January 24, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.