Build and Deploy a Rest API  Using Serverless, Express, and Nodejs
Tags: aws, javascript, webdev, beginners
Published: 2018-06-07 13:21:26

---
title: Build and Deploy a Rest API  Using Serverless, Express, and Nodejs
published: true
description: Rest API  Using Serverless, Express, and Nodejs
tags: aws,javascript,webdev,beginners
cover_image: https://cdn.pixabay.com/photo/2017/02/05/15/04/zen-2040340_960_720.jpg
---


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.


# What is  Aws Lambda?

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. 

![lamda](https://thepracticaldev.s3.amazonaws.com/i/blqwkpsbt4ld7iwxnstj.png)

### What are Requirements?

- [Aws Free Tier Account](https://aws.amazon.com/free/)

- Nodejs v8+


**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](https://www.youtube.com/watch?v=tgb_MRVylWw)

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.


{% gist https://gist.github.com/saigowthamr/574c4d8ffd9fe000049a2e6405964228 %}

We need to create a Model for the MongoDB database.
![](https://thepracticaldev.s3.amazonaws.com/i/ppcwl3jjaotqyhhcuia0.png)


**create a data.js file.**

```javascript
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.

{% gist https://gist.github.com/saigowthamr/e8cc131c8682862136e6cc88db2f831e %}

- 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
```

![offline](https://thepracticaldev.s3.amazonaws.com/i/wnafjb0fo3op83soinc1.png)


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.

```
sls deploy 
```

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

![deploy](https://thepracticaldev.s3.amazonaws.com/i/n8z561sg1a23m18nqy7a.png)


### How to Test the API Endpoints?

- **I'm using the Postman to test the API endpoints.**


## Post Method

![postmethod](https://thepracticaldev.s3.amazonaws.com/i/q54tl0qzuv0qie8gu70w.png)


## Get Method

![get](https://thepracticaldev.s3.amazonaws.com/i/4x4v86jwv08svi9hanv3.png)


### 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** 
{% gist https://gist.github.com/saigowthamr/e8cc131c8682862136e6cc88db2f831e %}


# How to Monitor and Track your Lamda Invocations?

For monitoring, Debugging and error detection of lambdas we are using [Dashbird](https://dashbird.io)

## 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](https://im4.ezgif.com/tmp/ezgif-4-b1b1b5a282.gif)

##### 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](https://dev.to/saigowthamr/how-to-build-your-first-serverless-webpage-using-aws-lambda-31de)