- We are only building the backend in Serverless Framework.
- The backend is defined using CloudFormation in Serverless Framework. While we use AWS CDK in SST.
Before we get started, here’s a link to the GitHub repo of the app that we will be building. Make sure to hang on to it for reference.
We also have the React.js frontend that connects to this — https://github.com/AnomalyInnovations/serverless-stack-demo-client
Now let’s get started with building our app!
In this chapter, we are going to set up the Serverless Framework on our local development environment.
Install Serverless globally.
$ npm install serverless -g
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 Framework 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
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 firstname.lastname@example.org --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.
serverless.yml and replace the default with the following.
service: notes-api # Create an optimized package for our functions package: individually: true plugins: - serverless-bundle # Package our functions with Webpack - serverless-offline - serverless-dotenv-plugin # Load .env as environment variables provider: name: aws runtime: nodejs12.x stage: prod region: us-east-1 functions: hello: handler: handler.hello events: - http: path: hello method: get
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!
We are also defining one Lambda function called
hello. It has a handler called
handler.hello. It follows the format:
So in this case the handler for our
hello Lambda function is the
hello function that is exported in the
Our Lambda function also responds to an HTTP GET event with the path
/hello. This will make more sense once we deploy our API.
You’ll notice the plugins that we’ve included —
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.
Let’s look at this in detail.