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

Share on facebook
Share on twitter
Share on linkedin
Share on reddit

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

Read more

Explore the site

More from the blog

Latest News