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.
Create a directory for our API backend.
$ mkdir notes-app-api $ cd notes-app-api
Install Serverless globally.
$ npm install firstname.lastname@example.org -g
At the root of the project; create an AWS Node.js service.
$ serverless create --template aws-nodejs
Now the directory should contain 2 files, namely handler.js and serverless.yml.
$ ls handler.js 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.
Install AWS Related Dependencies
At the root of the project, run.
$ npm init -y
This creates a new Node.js project for you. This will help us manage any dependencies our project might have.
Next, install these two packages.
$ npm install aws-sdk --save-dev $ npm install uuid --save
- aws-sdk allows us to talk to the various AWS services.
- uuid generates unique ids. We need this for storing things to DynamoDB.
Now the directory should contain three files and one directory.
$ ls handler.js node_modules package.json serverless.yml
- node_modules contains the Node.js dependencies that we just installed.
- package.json contains the Node.js configuration for our project.