We are going to be using AWS Lambda and Amazon API Gateway to create our backend. AWS Lambda is a compute service that lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no charge when your code is not running. And API Gateway makes it easy for developers to create, publish, maintain, monitor, and secure APIs. Working directly with AWS Lambda and configuring API Gateway can be a bit cumbersome; so we are going to use the Serverless Framework to help us with it.

The Serverless Framework enables developers to deploy backend applications as independent functions that will be deployed to AWS Lambda. It also configures AWS Lambda to run your code in response to HTTP requests using Amazon API Gateway.

In this chapter, we are going to set up the Serverless Framework on our local development environment.

Install Serverless

Install Serverless globally.

$ npm install serverless -g

The above command needs NPM, a package manager for JavaScript. Follow this if you need help installing NPM.

In your working directory; create a project using a Node.js starter. We’ll go over some of the details of this starter project in the next chapter.

$ serverless install --url https://github.com/AnomalyInnovations/serverless-nodejs-starter --name notes-api

Go into the directory for our backend api project.

$ cd notes-api

Now the directory should contain a few files including, the handler.js and serverless.yml.

  • handler.js file contains actual code for the services/functions that will be deployed to AWS Lambda.
  • serverless.yml file contains the configuration on what AWS services Serverless will provision and how to configure them.

We also have a tests/ directory where we can add our unit tests.

Install Node.js packages

The starter project relies on a few dependencies that are listed in the package.json.

At the root of the project, run.

$ npm install

Next, we’ll install a couple of other packages specifically for our backend.

$ npm install aws-sdk --save-dev
$ npm install uuid@7.0.3 --save
  • aws-sdk allows us to talk to the various AWS services.
  • uuid generates unique ids. We need this for storing things to DynamoDB.

Update Service Name

Let’s change the name of our service from the one in the starter.

Open serverless.yml and replace the default with the following.

service: notes-api

# Create an optimized package for our functions
  individually: true

  - serverless-bundle # Package our functions with Webpack
  - serverless-offline
  - serverless-dotenv-plugin # Load .env as environment variables

  name: aws
  runtime: nodejs12.x
  stage: prod
  region: us-east-1

The service name is pretty important. We are calling our service the notes-api. Serverless Framework creates your stack on AWS using this as the name. This means that if you change the name and deploy your project, it will create a completely new project!

You’ll notice the plugins that we’ve included — serverless-bundle, serverless-offline, and serverless-dotenv-plugin. The serverless-offline plugin is helpful for local development. While the serverless-dotenv-plugin will be used later to load the .env files as Lambda environment variables.

On the other hand, we use the serverless-bundle plugin to allow us to write our Lambda functions using a flavor of JavaScript that’s similar to the one we’ll be using in our frontend React app.

Let’s look at this in detail.