Community Blog Building a Serverless Application on Alibaba Cloud (Part 2)

Building a Serverless Application on Alibaba Cloud (Part 2)

In this second part of a two-part tutorial series, you will be building a sample serverless application on Alibaba Cloud.

By Nhi Ha, Alibaba Cloud Community Blog author

This blog is part of a two-part tutorial series on how to build a sample serverless application on Alibaba Cloud. This blog is the second part; in it, we will walk you through how to build a serveless application using a set of Alibaba Cloud services. The first part summarises some of the major concepts and the architecture behind the serverless application you will create in this part of the tutorial series.

Building a Serverless Application on Alibaba Cloud

From the first part we have seen that the serverless architecture can be realised using the following components:

  • Alibaba Cloud's Object Storage Service (OSS), which will store your static content such as HTML, CSS, Javascript files and images.
  • Alibaba Cloud's Function Compute, which will contain and execute the code for your application logic.
  • Alibaba Cloud's ApsaraDB for MongoDB, which will act as a backend database server of your system.

So, in the following sections, let's see how we can set up and configure those components.

Set up Object Storage Service (OSS)

Log in to the Alibaba Cloud console, you will find Object Storage Service on the left menu. If this is the first time you will be using OSS, it will ask you to activate the service. Click on the checkbox to agree with the agreement and enable the service.


Now, you can follow these steps to publish your website to OSS:

Step 1: Create a bucket

Click the + icon to create a new bucket.


Name the bucket and select a region for your bucket. There are three storage classes for an OSS bucket:

  • Standard: high reliability and availability for frequent access files
  • Infrequent Access (IA): long-term storage, infrequent access, lower storage price
  • Archive: long-term storage, very infrequent access, lowest storage price

Because in this tutorial this is a public website, I am going to set "Public Read" for the Access Control List.


Step 2: Upload files to the bucket

Creating folders and uploading files are pretty straight-forward. Just click on the buttons on the user interface.


Step 3: View the website

After you have finished uploading all files of your website to your bucket, click the Overview tab to view the address.


However, you may not see the website when you navigate to that URL. Instead you may see an Access denied message. This is because you haven't set the default homepage for the website. You can do it by clicking the Basic Settings tab and scroll down to the Static Pages section.


Now go back to your website URL you will see the homepage.

Map a custom domain name to an OSS bucket

Automatically, OSS assigns a domain name to your bucket in the following format:

[bucket name].[endpoint]

where the endpoint is oss-[region-id].aliyuncs.com

If you have your own domain name you can map it in the Domain Names tab.


After filling in the custom domain name, you can choose to switch on the CDN service.

That's everything you have to setup on the OSS side. On the Domain Name System (DNS) setting of the custom domain name, you have to add a CNAME record to map it to the bucket domain name (which is [bucket name].[endpoint]). If you do not know how to do it, please contact the service provider where you buy the domain name.

Later on, if you want to unlink that custom domain name from your bucket, click on Binding configuration and then click Unbind.


Set up Alibaba Cloud ApsaraDB for MongoDB

MongoDB is a popular name in the NoSQL family . 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.

Follow the below steps to set up your database:

Step 1: Create an instance

In the Alibaba Cloud 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. Alibaba Cloud will tell you how much the service cost you based on what you chose.

You can find more information about the instance parameters 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.

Step 2: IP whitelists and Connection String

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


ip.png1029x146 3.02 KB

After you set your IP whitelist, click Database Connection to see the connection parameters.


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

Create a 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 won't pay anything.

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 Alibaba Cloud console, select Function Compute and click the + icon to create a new service.


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

For a Function Template, select Empty Function.


In the Configure Triggers step, select HTTP Trigger and provide it a name. Other settings and configured to set are shown 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 function. Now click on the function you have just created and click the Code tab. This is where you provide your code for the function to run.


code.PNG630x572 23.4 KB

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.


And you've completed a function to insert a new record into MongoDB database triggered by a HTTP request. Function Compute is particularly useful in situations where we need to run code in response to a certain action or a specific event that has just happened.

More use cases of Function Compute include the following:

  • Monitoring user behavior on a website: for example, the user's mouse click will trigger a function to record what item the user has clicked on.
  • Image processing: when users upload images to OSS buckets, a function is triggered to create a thumbnail or to add a watermark to the image.
  • Automation tool: functions can be called on a schedule to process repetitive tasks like creating backups, generating reports or performing various other routine jobs.
  • Internet of Things (IoT) platforms: an Internet-of-Things (IoT) sensor can output trigger functions to control other devices in the network.

More about Function Compute

Above we have gone over the basic steps you can use to create and set up a Function Compute to execute your application logic. Function Compute is a very powerful product, which can help provide the capabilities necessary to run and scale out your system. It automatically can handle your scalability, high availability, security, performance requirements and do so all by its own. In addition, this service also provides log query, performance monitoring, and alarms, among several other useful features.

Function Compute can be a hassle to use because it demands a different logical structure and required that you have a well-designed implementation. That is, you need to pay extra attention while designing and coding your logic and workflow due to its event-driven and stateless system. The entire system now technically depends on separate functions calls triggered by different events. You need to build a separate state machine to store your application state. Another best practice is that you should refactor the big functions into smaller ones to optimise the execution time.

In practice, you may need to experiment and find out the optimal balance point for your system in terms of cost and performance. Providing more memory and processing power will execute code faster. However, after a certain threshold, more RAM and CPU doesn't reduce the execution duration but on the other hand increase the running cost.

More about Alibaba Cloud Function Compute can be found at Function Compute's Frequently Asked Questions Page.


In this tutorial, we have gone through the steps to set up a serverless application on Alibaba Cloud ecosystem. By using Object Storage Service, ApsaraDB for MongoDB and Function Compute, we have created a fully-managed event-driven compute service that allows you to run code more flexibly and reliably. No maintenance is required for this application. This model also brings you the convenience of real-time auto scaling and dynamic load balancing within milliseconds. Welcome to the new world of application development and service provision.

0 0 0
Share on

Alibaba Clouder

2,624 posts | 723 followers

You may also like


Alibaba Clouder

2,624 posts | 723 followers

Related Products

  • Function Compute

    Alibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.

    Learn More
  • ECS(Elastic Compute Service)

    An online computing service that offers elastic and secure virtual cloud servers to cater all your cloud hosting needs.

    Learn More
  • ApsaraDB for MongoDB

    A secure, reliable, and elastically scalable cloud database service for automatic monitoring, backup, and recovery by time point

    Learn More