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.
What are Requirements?
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.
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.
sls deploy
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?
- 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
How to Monitor and Track your Lamda Invocations?
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.
Top comments (4)
Sai, great guide!
Question: From your examples it looks like serverless (Lambda, Functions, or other) just sits right before the express layer and routes/passes all incoming HTTP traffic directly on to express. Given that express was handling the routes just fine by itself, before we introduced serverless, what's the purpose of it? Why do we want serverless in there, just passing requests on?
Thank you
Hi Sai, very great guide!
Only I have a warning use express and serverless to the same time, went someone create a service using aws like 50 endpoints, AWS put this problem: serverless.com/blog/serverless-wor...
So you have to change one framework or change to microservices.
pretty awesome! How can we go about getting our API's to reflect one of our subdomains?
I can't get the post and get requests to work when using an Atlas connection string. I keep getting 502 Bad Gateway with an internal server error.