Deploying a NodeJS - Express API to AWS Lambda
Juan P. Lima
Posted on January 19, 2023
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 <- 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:
On our ping.services.js we will have our PingService class with a method called getPing that will return the word "pong":
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:
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:
And last but not least, we'll set up our index.js on the routes folder, this is what it will look like:
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:
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:
Finally, we can now create a script on our package.json so we can check if everything is working as expected:
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:
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 <-
After doing so, you can run serverless deploy , after doing so, you can see your function on AWS Lambda function list:
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:
You can grab your API endpoint and test it using your favorite API Client, I'll be testing it using Insomnia:
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 <-
GitHub repository with our example: https://github.com/juanpireslima/nodejs-api-lambda
Wish you all the best.
Posted on January 19, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.