How to Build a Serverless API with Lambda and Node.js

Serverless technologies enable developers to concentrate on what the application does without the hassle of managing where it runs and how it scales. The cloud provider manages infrastructure, simply upload the applications, and the provider handles the rest.

This article highlights the benefits of going serverless by walking through creating a serverless REST API using AWS Lambda and Node.js.

Setting Up the Local Environment

This tutorial requires the following dependencies:

Now that the environment is almost ready, it’s time to initialize our project. Let’s make a new folder for the project, and from within the folder, do the following:

First, create a boilerplate serverless project to provide a template:

create –template aws-nodejs –name trendmicro

Then, install serverless offline into our project:

npm install serverless-offline –save-dev

We now have a project containing the serverless.yml and handler.js files. The serverless.yml file is a YAML configuration file that describes our serverless application’s functions, resources, plugins, and other necessary config information. The handler.js file is an example handler that provides a “hello world” REST API function.

The final step of our setup is to add the serverless-offline package we just installed to the YAML config file as a plugin. The following is a simplified version of the YAML file with the serverless-offline plugin.

service: trendmicro

frameworkVersion: ‘2’

provider:
 name: aws
 runtime: nodejs14.x
 lambdaHashingVersion: 20201221
 stage: dev
 region: eu-west-1

plugins:
 – serverless-offline

functions:
 hello:
  handler: handler.hello
  events:
    – httpApi:
      path: /hello
      method: get

The service property specifies the service name, and the provider section details which service provider to use — in our case, AWS — and any configuration properties.

The plugins section specifies which plugins to use. For now, we just require the serverless-offline plugin. We will discuss why we need this shortly. Finally, the functions section details which functions should be available in our AWS Lambda function and their configuration. Again, we go into more detail about this configuration in the next section.

Building the API

Now we have the boilerplate for a straightforward REST API that exposes an endpoint at /hello. We must look at our handler to define what happens when we hit that endpoint.

The template created the handler.js file for us. We can see from the YAML file that it should contain a hello function.

‘use strict’;

module.exports.hello = async (event) => {
return {
   statusCode: 200,
   body: JSON.stringify(
    {
       message: ‘Go Serverless v1.0! Your function executed
       successfully!’,
       input: event,
    },
    null,
    2
  ),
};

// Use this code if you don’t use the http event with the LAMBDA-PROXY integration
// return { message: ‘Go Serverless v1.0! Your function executed successfully!’, event };
};

As expected, the file exports a function called hello. This function may look straightforward, but it provides us with necessary information about creating REST functions using serverless APIs. First, our function must take an event parameter. This event triggers the Lambda function. In our case, it is a GET request to the relevant URL.

Second, the function shows us the required structure of the object that the GET request should return. The object requires an HTTP status code and a body that must be a string, so if we would like to return a JSON object as the body, we must convert it to a string first.

It is that simple. This function can perform any required business logic, and if it returns the object in the correct format, we have a functioning REST API. We run it in offline mode using the serverless-offline plugin to test it. This approach enables us to test our API logic locally before deploying the Lambda function to AWS. To run our serverless API offline, we simply run serverless offline.

Read More HERE