How to build an API endpoint with Node.js, AWS Lambda and AWS RDS PostgreSQL DB [Part 1]

Timo Wagner
3 min readDec 14, 2018

--

Photo by fabio

Intro

The new hype around serverless computing (SC) aka Function-as-a-Service (FaaS) made me curious. So I gave it a try to check it out. Therefore I implemented a basic example CRUD application for item entities.

The application will provide an API endpoint to create, read, update and delete an item (ANY /item). This will be realized with Node.js and express.js. The backend will be deployed as an AWS lambda function provided by the AWS API Gateway. In combination with the claudia.js framework, we are able to set up the API endpoint more conveniently. To be able to store the item entities, we will create a PostgreSQL DB via AWS RDS. For the ease of use, we will fall back on the framework sequelize.js.

For easier reading, this tutorial will be separated into three parts. The first part (this one) comprises the initialization of the CRUD application. The second part will focus on the development of the API endpoints and the local database setup. The third part explains the deployment details at Amazon’s AWS. So if you already have a Node.js API application with sequelize.js running, you can jump to the deployment part (part 3).

The following series assumes that the reader has a basic understanding of the command line, REST web services, Node.js, express.js, and PostgreSQL. You should also be registered at Amazon’s AWS. AWS Lambda offers free tiers. AWS RDS offers also free tiers but only for new customers. Nevertheless, for experimenting and getting familiar with the platform, it is cheap :)

Get started

First, we will focus on implementing the Node.js API. For the following, it is assumed that you already got Node.js installed. Then create a project folder (e.g. mkdir aws-example) and initialize the project: npm init -y. Then we install some dependencies: npm i -S express body-parser morgan and next npm i -D nodemon. In the package.json file add the following line under the scripts entry: "start:dev": "nodemon app.js". Nodemon will now be able to restart automatically every time you make a code change (good for local development). Now create the entry file of our API. Call it e.g. app.js and add this code:

The initial app.js as the entry point for the API application

To test the app run npm run start:dev and visit http://localhost:8000. This is the first initialization of our API. The GET request listens to any endpoint (*). Next, it is time to model our item entities.

The item entity and Sequelize

This section will set up the item entity or model for our API application. Therefore we will use sequelize.js. So install the sequelize command line interface globally: npm i -g sequelize-cli . Next, create the .sequelizerc file in your root project folder. Add the following configurations into that file:

Folder path configuration for the sequelize framework

This defines necessary folders and paths used for the Object-Relational Mapping (ORM) in the Node.js framework. Now it is time to install sequelize and postgres with npm i --save sequelize pg pg-hstore. This allows us to initialize the folder structure defined by the .sequelizerc file. Thus run sequelize init in your root project folder.

We can now create our item model with the sequelize command line interface: sequelize model:create --name Item --attributes item_id:integer,name:string,weight:float. As you can see we gave it the attributes: item_id (integer), name (string) and weight (float). Sequelize will automatically add an id attribute, as well as createdAt and updatedAt attributes (both dates).

The item model entity can be found in …/server/models and …/server/migrations. The migrations part will create the item Table (notice the pluralization and capitalization to Items). The models part maps the internal items object to the database object (notice the capitalization to Item).

For more details on using sequelize and Node.js please go back to other tutorials, as this is just a small example project for an AWS deployment. You could e.g. also define associates (relations between different models, e.g. many-to-one). Hence, take a look into this article.

Now we have a basic application running and the item models created. In the next part of this tutorial series, we will setup a PostgreSQL DB and the actual API endpoints to test our application locally. In the last part of the series, we are going to deploy our app to the AWS ecosystem.

--

--