AWS Lambda supports Node.js v10.x and v12.x. However, the supported syntax is a little different when compared to the more advanced ECMAScript flavor of JavaScript that our frontend React app supports. 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.

Additionally, our frontend React app automatically supports TypeScript, via Create React App. And while we are not using TypeScript in this guide, it makes sense to have a similar setup for your backend Lambda functions. So you can use it in your future projects.

To do this we typically need to install Babel, TypeScript, Webpack, and a long list of other packages. This can add a ton of extra config and complexity to your project.

To help with this we created, serverless-bundle. This is a Serverless Framework plugin that has a few key advantages:

  • Only one dependency
  • Supports ES6 and TypeScript
  • Generates optimized packages
  • Linting Lambda functions using ESLint
  • Supports transpiling unit tests with babel-jest
  • Source map support for proper error messages

It’s automatically included in the starter project we used in the previous chapter — serverless-nodejs-starter. For TypeScript, we have a starter for that as well — serverless-typescript-starter.

However, if you are looking to add ES6 and TypeScript support to your existing Serverless Framework projects, you can do this by installing serverless-bundle:

$ npm install --save-dev serverless-bundle

And including it in your serverless.yml using:

plugins:
  - serverless-bundle

To run your tests, add this to your package.json.

"scripts": {
  "test": "serverless-bundle test"
}

Optimized Packages

By default Serverless Framework creates a single package for all your Lambda functions. This means that when a Lambda function is invoked, it’ll load all the code in your app. Including all the other Lambda functions. This negatively affects performance as your app grows in size. The larger your Lambda function packages, the longer the cold starts.

To turn this off and it to ensure that Serverless Framework is packaging our functions individually, add the following to your serverless.yml.

package:
  individually: true

This should be on by default in our starter project.

Note that, with the above option enabled, serverless-bundle can use Webpack to generate optimized packages using a tree shaking algorithm. It’ll only include the code needed to run your Lambda function and nothing else!

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