Building a Serverless REST API with Function Compute and MongoDB

This tutorial will show you how to deploy a MongoDB database on Alibaba Cloud and use Function Compute to develop a back-end API system to interact with the database.

Overview of NoSQL and MongoDB

Relational databases have been selected as the primary system to manage data in software development for a long time. Its ACID principals promote the data persistency, transaction integrity and concurrency control. Over the last few years, NoSQL (Not only SQL) has become popular. This model solves the impedance mismatch between the relational data structures (tables, rows, fields) and the in-memory data structures of the application (objects). Most importantly, NoSQL is designed to scale horizontally which makes it an excellent choice for modern web applications.

NoSQL could be categorized into 4 groups:

  • Key-Value
  • Document
  • Column family
  • Graph

MongoDB is the most popular system within the document database group. As defined on,

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

Alibaba Cloud ApsaraDB for MongoDB

MongoDB databases can be deployed on Alibaba Cloud via its ApsaraDB for MongoDB service. Users can select among 3 pricing schemes:

  • Subscription (Replica Set)
  • PAYG (Pay-As-You-Go) (Replica Set)
  • PAYG (Sharding)

Replication and sharding refers to the data distribution models:

  • Replication copies data across multiple servers. The same piece of data is stored in different places. ApsaraDB for MongoDB uses a 3-server replica set. Replication may help with horizontal scaling of reads.
  • Sharding distributes data across multiple machines so different machine contains different subset of data. This model allows for horizontal scaling of writes.

Create an instance

In the AliCloud console, click on Products and you will see ApsaraDB for MongoDB under the ApsaraDB group. Alternatively, you can use the search box to filter the desired service.

Select the pricing scheme, the region, the server specification, and set a password for your database. AliCloud will tell you how much the service cost you based on what you chose.

More info about the instance parameters is here.

Note: if you are using a free trial account, remember to select a subscription instance. PAYG instances do not include in the trial program.

Set IP whitelists

To ensure database security AliCloud automatically block all access to the database. You have to specify IP addresses in the whitelist to gain access to the target instance.

Connection string

After you set your IP whitelist, click on “Database Connection” to see the connection parameters


Use this connection string to connect to the database in your Nodejs code.

Function Compute

Function Compute lets you run code without provisioning or managing servers. This service prepares computing resources for you and runs your codes on your behalf elastically and reliably. You only pay for resources actually consumed when running the codes. If your code isn’t executed, you don’t pay.

Function Compute runs your code in response to events. When the event source service triggers an event, the associated function is automatically called to process the event.

From the AliCloud console, select Function Compute and click on the “+” icon to create a new service


In the newly created service, click on the “+” icon to create a new function. You will go through a multi-step wizard to select options for your function.

For Function Template, select “Empty Function”

In the “Configure Triggers” step, select “HTTP Trigger” and give it a name. Other settings are as the image below

In the “Configure Function Settings” step, set a name for your function and select “nodejs6” as runtime


Click “Next” in the last two steps to finish the wizard and create the fucntion.

Program your function

Click on the function you have just created and click on the “Code” tab. This is where you provide your code for the function to run

Use the connection string from your MongoDB server.

Also in this screen you can view the HTTP trigger that will invoke your function. You can also run the trigger to test you function here.


In this tutorial we have learnt about NoSQL database with MongoDB as a popular example. AliCloud provides its ApsaraDB for MongoDB service to those who want to run MongoDB servers on its cloud. The tutorial then moves to discuss the Function Compute service as a new way to build your application following the emerging Serverless architecture. It shows an example of a Nodejs function triggered by an HTTP request to connect to the MongoDB database and perform an “insert” command.