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
- 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 https://github.com/AnomalyInnovations/serverless-nodejs-starter --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.yml and replace the default with the following.
service: notes-app-api # Create an optimized package for our functions package: individually: true plugins: - serverless-bundle # Package our functions with Webpack - serverless-offline provider: name: aws runtime: nodejs8.10 stage: prod region: us-east-1
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-offline that we have included. The first plugin we talked about above, while the serverless-offline is helpful for local development.
We are also using this option:
# Create an optimized package for our functions package: 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.
And now we are ready to build our backend.