How to Build a Serverless Backend with AWS Lambda and Node.js

Get familiar with function-as-a-service (FaaS), deploy a demo on a AWS Lambda serverless computing platform, hook up a MongoDB database-as-a-service to a serverless REST API, and more.

Aman Mittal
Nov 12, 2018 · 12 min read
Image for post
Image for post

What is AWS Lambda?

In order to build and deploy a backend function to handle a certain operation, I am going to start with setting up the service provider you are going to use to follow this article. AWS Lambda supports different runtimes such as Node.js, Java, Python, .NET Core and Go for you to execute a function.

  • Ephemeral disk space: 512MB
  • Max execution duration: 300 seconds
  • Compressed package size: 50MB
  • Uncompressed package size: 250MB
Image for post
Image for post

Setting up AWS Lambda

In order to setup a Lambda function on AWS, we need to first register an account for the access keys. Use your credentials to login or signup a new account on console.amazon.com and once you are through the verification process you will be welcomed by the following screen.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Creating a Serverless Function

We are going to use install an npm dependency first to proceed and scaffold a new project. Open up your terminal and install the following.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
  • handler.js where we will write our handle function
  • serverless.yml contains the configuration
Image for post
Image for post
Image for post
Image for post

REST API with Serverless Stack

In this part of the tutorial, I will show you how to hook up a MongoDB database as a service to a Serverless REST API. We are going to need three things that will complete our tech stack. They are:

  • Node.js
  • MongoDB Atlas

Creating a database on MongoDB Atlas

We will start by creating a database on the MongoDB Atlas. Login to the site and create an account if you do not have it already. We just need a sandbox environment to get hands-on experience so we must opt for free tier. Once you have your account set up, open up your account page and add a new organization.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Building the API

Next, we install all the necessary dependencies in order to create the API.

Defining the Handler Functions

If you are familiar with Node.js and Express framework you will notice there is little difference in creating a controller function that leads to the business logic of a route. The similar approach we are going to use to define in each handler function.

Connecting MongoDB

We need to create a connection between the database and our serverless functions in order to consume the CRUD operations in real-time. Create a new file called db.js in the root and append it with following.

Using Dotenv and Environment Variables

Protecting our keys and other essentials is the first step to a secured backend application. Create a new file called variables.env. In this file, we will add our MONGODB connection URL that we have already used in db.js as a process.env.DB. The good thing about environment variables is that they are global to the scope of the application.

Image for post
Image for post
Image for post
Image for post

Deployment

All you have to do is run the deploy command from the terminal.

Image for post
Image for post

Image for post
Image for post

Building A Web Or Mobile App?

Crowdbotics is the fastest way to build, launch and scale an application.

Crowdbotics

The fastest way to build your next app.

Aman Mittal

Written by

👨‍💻Developer 👉 Nodejs, Reactjs, ReactNative | Tech Blogger with 1M+ views at Medium| My weekly dev newsletter 👉 tinyletter.com/amanhimself

Crowdbotics

The fastest way to build your next app.

Aman Mittal

Written by

👨‍💻Developer 👉 Nodejs, Reactjs, ReactNative | Tech Blogger with 1M+ views at Medium| My weekly dev newsletter 👉 tinyletter.com/amanhimself

Crowdbotics

The fastest way to build your next app.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store