Build and Deploy a Rest API Using Serverless, Express, and Nodejs

June 09, 2018 0 Comments

Build and Deploy a Rest API Using Serverless, Express, and Nodejs



Today I'm showing how to Build and deploy an express/Node Rest API using serverless lambda.

We know Serverless is growing so fastly and a lot of companies are saving money by using Serverless computing.

Amazon web services offer us wide variety of services to build and deploy Small-scale to Large-Scale Applications.

AWS Lambda runs your code without provisioning or managing servers it means you don't need to buy or manage a server. You only pay whenever your code runs or whenever a user visits your site.


What are Requirements?

I'm using the serverless framework Cli to create and deploy apps.

Open your terminal

npm install -g serverless sls login // SLS is a shortcut of serverless 

After sls login, You need to configure Your Aws Credentials with a serverless framework.

Get Aws Credentials

Once It is done Your are good to go.

Creating and Deploying the Rest API endpoints

Create a Template in Your Working Directory

serverless create --template aws-nodejs 

The above command generates the boilerplate.

Now we need to initialize the Package.json file and install

some dependencies.

npm init -y // generates package.json file npm i -s body-parser,cors,express,mongoose",serverless-http 

Open Your app folder in your Favourite code editor.

Create an app.js file and import the requirements for the database i'm using the mongodb.

We need to create a Model for the MongoDB database.

create a data.js file.

const mongoose = require('mongoose'); const Users = mongoose.Schema({ name: String, age: Number, 
}) const User = mongoose.model('User', Users); module.exports = User;

This is our simple User model.

Open your serveless.yml and add the below code.

  • runtime nodejs8.10 why because Aws lambda currently supports v8.10.

  • handler: we need to define the app.handler it means please look into app.js file.

  • serverless-offline plugin helps us to run your app into the local environment.

Open your Terminal and run.

sls offline start // to run the code in local environment 


Wow, our code is running without any errors.

How to deploy the Code into Aws Lambda?

  • It is very easy serverless framework does all things in the background

Open your terminal and run.

Once you run above command after some time your API endpoints are visible in your terminal.


How to Test the API Endpoints?

  • I'm using the Postman to test the API endpoints.

Post Method


Get Method


How to deploy your existing rest API?

  1. Open your app.js file or main.js file and add these export.

const serverless = require('serverless-http'); 
const express = require('express');
const app = express(); express code module.exports.handler = serverless(app);

2.Setup your serverless.yml

For monitoring, Debugging and error detection of lambdas we are using Dashbird

Why Dashbird?

  • Dashbird helps us actively monitoring the health and errors.

  • One main thing about Dashbird is its user-friendly Interface.

  • Dashbird visualizes all your AWS Lambda metrics like
    memory utilization, invocation count, and execution duration.

DashBird Interface

Dashbird monitoring

Hope you guys enjoyed if you have any doubts feel free to ask.

Other Interesting Posts on Serverless

How to Build Your First Serverless Website

Tag cloud