AWS Lambda recently added support for Node.js v8.10 and v10.x. The supported syntax is a little different when compared to the frontend React app we’ll be working on a little later. It makes sense to use similar ES features across both parts of the project – specifically, we’ll be relying on ES imports/exports in our handler functions. To do this we will be transpiling our code using Babel and Webpack 4. Also, Webpack allows us to generate optimized packages for our Lambda functions by only including the code that is used in our function. This helps keep our packages small and reduces cold start times. Serverless Framework supports plugins to do this automatically. We are going to use an extension of the popular serverless-webpack plugin, serverless-bundle.

All this has been added in the previous chapter using the serverless-nodejs-starter. We created this starter for a couple of reasons:

  • Generate optimized packages for our Lambda functions
  • Use a similar version of JavaScript in the frontend and backend
  • Ensure transpiled code still has the right line numbers for error messages
  • Lint our code and add support for unit tests
  • Allow you to run your backend API locally
  • Not have to manage any Webpack or Babel configs

If you recall we installed this starter using the serverless install --url --name my-project command. This is telling Serverless Framework to use the starter as a template to create our project.

In this chapter, let’s quickly go over how it’s doing this so you’ll be able to make changes in the future if you need to.

Serverless Webpack

The transpiling process of converting our ES code to Node v10.x JavaScript is done by the serverless-bundle plugin. This plugin was added in our serverless.yml.

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

service: notes-app-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: nodejs10.x
  stage: prod
  region: us-east-1

The service option is pretty important. We are calling our service the notes-app-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 — serverless-bundle, serverless-offline, and serverless-dotenv-plugin, that we have included. The first plugin we talked about above, while the serverless-offline is helpful for local development and serverless-dotenv-plugin that we’ll use later loads the .env files as Lambda environment variables.

We are also using this option:

# Create an optimized package for our functions
  individually: true

By default, Serverless Framework creates one large package for all the Lambda functions in your app. Large Lambda function packages can cause longer cold starts. By setting individually: true, we are telling Serverless Framework to create a single package per Lambda function. This in combination with serverless-bundle (and Webpack) will generate optimized packages. Note that, this’ll slow down our builds but the performance benefit is well worth it.

Now we are ready to write our backend code. But before that, let’s create a GitHub repo to store our code.