Deploying your NestJS API on AWS Lambda
Tags: node, aws, beginners, javascript
Published: 2023-01-24 21:28:34

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 <-](https://dev.to/juanpireslima/aws-lambda-what-it-is-how-to-use-when-to-use-and-when-not-to-3fod)**

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uy506fpstc2kbe6uuf3k.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2545y1facqck1v5f0rmh.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z87zddgi1ucfxyyvgo06.png)

## 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 <-](https://www.serverless.com/framework/docs/providers/aws/guide/credentials/)**

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9u9e2d5ztx33ev6lhlm3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffs4txawq0ijz2l3w8yv.png)

With that link, you can use your favorite API Client to test if it's working as expected:
![API Client testing my Lambda function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2w74g81by5ckncrvk71e.png)


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<-](https://dev.to/juanpireslima/aws-lambda-what-it-is-how-to-use-when-to-use-and-when-not-to-3fod)**
**[-> Deploying a NodeJS - Express API to AWS Lambda <-
](https://dev.to/juanpireslima/deploying-a-nodejs-express-api-to-aws-lambda-hgc)**
Github repository with my example **[-> Click Me <-](https://github.com/juanpireslima/nestjs-lambda)**

NestJS documentation about serverless framework -**[> Click Me <-](https://docs.nestjs.com/faq/serverless)**