Deploying a NodeJS - Express API to AWS Lambda
Tags: node, aws, javascript, beginners
Published: 2023-01-19 00:14:49

If you don't know what AWS Lambda is or would like to learn a little more about it, I highly recommend that you read the first article of my AWS Lambda series, 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) <- On this second article, I'm going to teach you guys how you can deploy your NodeJS-Express API to AWS Lambda.

## Starting your project
In this section, I'm going to start a **new NodeJS-Express project, if you already have one, feel free to jump this section. You can also check the source code on my GitHub that will be at the end of this post.**

After creating a folder for your project(name it whatever you like) you can execute `npm init -y` to generate your package.json, after doing so, you should install **express** on your project, you can do it by executing `npm install express`, now we can create our first router, controller, and service.
In this example I'll be creating a ping route, controller, and service. Your folder should look something like this:
![Our project's folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7tmmd05nugx9kdxqct6.png)

On our ping.services.js we will have our **PingService class** with a method called **getPing** that **will return the word "pong":**
![ping.services file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mjoxomck24fhfn89ava.png)
Now on our ping.controller.js, we'll have our **PingController class** with a method the same name as the service one, and it will call the **PingService's getPing method, returning the http 200 status and its result on a json:**
![ping.controller file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3n557aboyxglqflk11t.png)
Then on our ping.routes, we'll have a **get route** that **will be handled by our PingController's method getPing**, your file will look like this:
![ping.routes file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24a6iup1itns78dw03ld.png)
And last but not least, we'll set up our index.js on the routes folder, this is what it will look like:
![routes' folder index.js file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bluy6wttvrqxv6np6uvf.png)
Now **we're ready to go to the next step.**

## Setting up serverless framework

Serverless is the name of the framework that will help us deploy our project to the AWS Lambda, first, you should install "serverless-http" dependency on your project(`npm install serverless-http`), then you'll need to globally install serverless framework by typing `npm install serverless -g` then you'll need to install a devDependency called "serverless-offline" you can do it with `npm install serverless-offline -D`.

Now we can create our handler function that will handle our HTTP requests, on my project's root directory, I'll create a file called lambda.js, there we'll create our express application and export our handler that will contain our app wrapped to work with AWS Lambda-API Gateway:
![lambda.js file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp8n7bsfzkwrwdwob2gb.png)
Now we'll need to set up our serverless settings, on our root directory, we should create a file named "serverless.yml", there you'll have the runtime your project will be running in, the name of your service, the region your function will be deployed… it will look like this:
![serverless.yml file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r9p3nykga49rczeu7yd.png)

Finally, we can now create a script on our package.json so we can check if everything is working as expected:
![package.json file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0al0owiuqnssbs2jojdz.png)
You can use nodemon if you like, this function will run our serverless-offline using the settings on our serverless.yml, we'll be running our project the same way lambda will, by running this script, our server will start running and we'll be able to make requests, I've tested mine using Insomnia:
![Project running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16ub9kqd3hv9y6zjvrir.png)

As you can see, mine is working without any issues.

## Deploying to AWS Lambda
Now it's time for us to deploy our project to AWS Lambda, for it, you'll have to configure your credentials on serverless, I won't be covering here how you can do it, but you can found out in this guide: -> [Click Me](https://www.serverless.com/framework/docs/providers/aws/guide/credentials/) <-

After doing so, you can run serverless deploy , after doing so, you can see your function on AWS Lambda function list:
![AWS Lambda function list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnhb5yprbdk9t98md3rf.png)
Serverless framework will have created the function for you with an API-Gateway trigger and with all settings you have set, by clicking on your function, you can see its details, and by going to configuration -> Triggers, you'll find the API-Gateway trigger with your API URL:
![Our Lambda function info](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbysj2k41o8fd5o4mcvp.png)
You can grab your API endpoint and test it using your favorite API Client, I'll be testing it using Insomnia:
![Test using API-Gateway endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ricrj6m8qawusote0st0.png)

As you can see, my API is working just fine.

## Conclusions
As you can see, it's that easy to deploy your NodeJS-Express API to AWS Lambda, feel free to comment any suggestion or feedback, if you haven't read the first article of the series, you can check it out -> [here](https://dev.to/juanpireslima/aws-lambda-what-it-is-how-to-use-when-to-use-and-when-not-to-3fod) <-
GitHub repository with our example: https://github.com/juanpireslima/nodejs-api-lambda

**Wish you all the best.**