Interested in how to quickly build manageable serverless REST APIs? In this article, I will be demonstrating how to accomplish this for NodeJS REST APIs with the Serverless Framework. For this demonstration, we will be building a simple API that manages the users for our demonstration application. To follow along you should have the pre-requisites:
- An AWS account and developer credentials
- Node and npm installed locally
I think at this point that serverless computing has been around long enough that we are all well aware that serverless doesn’t mean there aren’t any servers. It just means you don’t have to worry about managing and maintaining your own servers. And if you’ve been unfortunate enough to have to do that then you know how painful that can be at times.
The pricing model is also good for new or lightly used applications because you are charged by actual use rather than for reserved capacity. So let’s say your API services 1000 requests a month. You are only going to be charged for the computing time it took to run those requests. Alternatively, if you are deploying your application on an EC2 virtual machine you will be charged for the month of having that VM running 24/7 to serve your API.
Serverless also handles all of the hardware scaling automatically without you having to intervene at all. So if your API is getting hit particularly hard during a holiday shopping period or something along those lines you don’t have to worry about scaling up to more VM instances or upgrading hardware. All of that is managed for you within serverless services.
All cloud providers have some form of serverless service available. In this article, we will be using AWS’s Lambda to deploy our serverless REST API. If you’d like to read more about Lambda itself check out the documentation here.
You can set up and manage Lambda through the AWS management console, but the simpler way to manage your serverless code is to use a framework to do all of that work for you. For this article, we will be using the Serverless Framework. With this framework, you can automate all of the deployments and management of the AWS resources you need for your Lamda code. You can discover more about the Serverless Framework on their site.
For our example, we will be using DynamoDB for storing data. DynamoDB is AWS’s managed version of MongoDB which is a no-SQL database. It is basically just storing JSON objects as documents which makes it ideal for working with NodeJS applications. It is also as I mentioned completely managed so much like Lambda you do not need to worry about maintenance and scaling. You can read more about it in the AWS documentation here.
Example API & DynamoDB
Now that we have gone over the background of why we would choose serverless for our REST API we are going to walk through building an example. First, we will need to install the Serverless Framework CLI. Run the following command in a terminal to install. Note you will need to have NodeJS and npm installed already before installing.
$ npm install -g serverless
With the CLI installed now we can create a new project. Run the following command and it will walk through a few prompts.
It will ask you if you want to create a new project and the answer will be yes. Then it will prompt you to select the type of project and we will select “AWS Node.js”.
Then after making the type of project selection, it will ask for a name for the project and then after that, it will generate the project for us.
There are several ways to set up your AWS credentials, but for example, I will use the Serverless CLI to provide those credentials. To do this you will need an AWS account and an IAM user with a set of access keys for that user. Use the following command and replace the keys with your own.
$ serverless config credentials --provider aws --key YOUR_KEY_HERE --secret YOUR_KEY_SECRET_HERE
Note that this command creates a file with a default profile at ~/.aws/credentials so if you need to update your keys later or add a new profile that is the place to do it. Just for reference, the contents of that file will look like the following.
Also, note that if you have multiple profiles set up in this credentials file you can specify which one to use when you are deploying using the CLI. For example, you would use the following command to specify the default profile. Note you don’t have to specify to get the default profile, but just a demonstration in case you wanted to use a different profile.
$ serverless deploy --aws-profile default
Within our project folder, we’ll need to create an empty package.json file and install a couple of dependencies.
$ npm init -y
$ npm install --save uuid
First, we will create a file for all of our route handlers. Create a folder called routes/ and in that folder create a file called users.js. Add the following code in that new file.
This is the code for all of the route handlers for the API. We have a DynamoDB client that connects to our database and a route handler for each of the CRUD operations. Most of this is pretty straight forward. Depending on the operation we run a query against the database and then send the response to end the request.
Next up we need to connect these to endpoints in our serverless.yml file. Open up that file and replace the contents with the following.
This file is what Serverless uses to set up resources in AWS when we run the
serverless deploy command. You can see we are setting up two things here. The first is our instance of DynamoDB. If you take a look at the route handlers you can also see that this table name is exposed as an environment variable that is consumed by our DynamoDB client. We also define an entry for each of our Lambda functions. We have a function for each endpoint and we map them all to the right handler in the route handler file we just completed.
That completes our code. We are ready to deploy our functions to AWS. Run the following command to start the deployment.
$ serverless deploy
This will take a bit to set everything up and then will print out the locations for the API endpoints in the terminal once it is finished.
Now that we have the endpoints we can test them all out in an HTTP client. See some examples below.
After you are all done playing around with the API remember to run the following command to tear everything down from our example app in AWS.
$ serverless remove
Serverless computing is great for small applications or new prototypes to help you move very quickly. And I hope this demonstration of how to do this with the Serverless Framework can make things even easier on developers. Thank you for reading!