Community Blog Using Log Service with Alibaba Cloud's Function Compute

Using Log Service with Alibaba Cloud's Function Compute

In this tutorial, we will show you how to create an Alibaba Cloud Log Service project which triggers an Alibaba Cloud Function Compute function.

In this tutorial, we will show you how to create an Alibaba Cloud Log Service project which triggers an Alibaba Cloud Function Compute function.

What Is Alibaba Cloud's Function Compute?

Alibaba Cloud's Function Compute is a fully-managed, event-driven compute service for serverless applications. Developers focus on writing and uploading code without having to worry about building the underlying infrastructure or maintaining complicated server architectures.

Alibaba Cloud's Function Compute service has a range of applications on offer, including API gateways, data lake analysis, log stores and backups, web crawlers and image recognition, to name just a few. With Function Compute, developing is convenient and reliable. Function Compute supports many different programming languages including Java, Python, PHP, and NodeJS.

Alibaba Cloud's Function Compute provides real-time auto scaling and dynamic load balancing for managing heavy traffic bursts within millisecond timeframes. The compute resources offered make code flexible and reliable. Furthermore, Function Compute offers a Pay-As-You-Go option which means that you only have to pay for consumed resources. No fee is incurred if the code doesn't run. Moreover, the code run duration is measured in milliseconds.

A few common business scenarios for Alibaba Cloud's Function Compute include analysis and management of media assets, for example integrating a range of services that run an elastic and highly available backend video system. Another is building a serverless backend that triggers Function Compute code to render dynamic webpages and static webpages housed in Alibaba Cloud's Object Storage Service. Web applications like these also have the capability to connect to external APIs when Function Compute is integrated with Alibaba Cloud's API Gateway. Another interesting business scenario is implementing Function Compute to manage real time IoT message processing and monitoring of data streams.

What Is Alibaba Cloud's Log Service?

Alibaba Cloud's Log Service is a logging service available for use with Alibaba Cloud products and services. Applications built on Alibaba Cloud connect to a ready-made Log Service Logstore which efficiently configures and manages log collection and maintenance. Log Service is the Alibaba Cloud one stop shop for all Alibaba Cloud product logging requirements.

Alibaba Cloud's Log Service is a powerful solution for collecting, consuming, transferring, querying, and analyzing massive amounts of logging data with minimal development effort. The improvements to the operational efficiency of log management for all kinds of scenarios, including big data scenarios, means that developers can focus more of their time on their business requirements.

Alibaba Cloud's Log Service provides a user-friendly interface for accessing the real-time log data it consumes from services and applications. Log data is viewable as metrics, events, binary logs, text logs, and click data. Log Service is ideal for database ETL (extract, transform, load), streaming, monitoring and alarm, machine learning and iterative computing.

How Do Log Service and Function Compute Fit Together?

With Alibaba Cloud's Function Compute, you can create a function that connects to a Log Service project in the same region. The Function Compute function sets a trigger on the Log Service and then listens for events coming in from the Log Service project.

Once the Log Service job is up and running, it works on a timer principle which polls Alibaba Cloud Logstore shard information at regular points in time – every second, for example. The Function Compute trigger executes the function configured in Function Compute on a regular basis or when given specific input parameters.


You will need an Alibaba Cloud account. If you don't already have one, head over to the Free Trial page to get $300-1,200 worth of Alibaba Cloud products to play around with in the Alibaba Cloud Free Trial.

Let's get started.

Create a Log Service

Go to the Alibaba Cloud products page and click through to Log Service.


Click Create Project.


Enter a Project Name and a Region and click Confirm.


In the popup, click Create to create a Logstore.


In the Logstore configurations, give the Logstore a name and apply the required settings.

We have configured Web Tracking, Permanent Storage, Automatic Sharding, and Public IP logging.


We now have a Logstore.

Click Data Import Wizard.


The Data Import Wizard allows you to connect up your application data to the Logstore you created. As you can see, there are many different options here.


Let's go back to the Logstore.


Let's add a new Logstore we need for the Function Compute function.

Click Create and follow the steps as before.


When you have two Logstores, click through to Endpoints.


Make a note of the Endpoint for your region. We will need this information for Function Compute.

Silicon Valley Endpoint = us-west-1.log.aliyuncs.com


Create a Function Compute Function

Click through to the Function Compute console from the Products page.


In the Service page, click the Services add button.


Give the Service a name, slide open the Advanced Settings, and set Internet Access.


In Log Configs, set Log Project and LogStore.

In the Role Config, add the AliyunLogFullAccess Role and click Authorize.


Check the details and click Confirm Authorization Policy, then click OK.


Now, back in the Function Compute Service details click add Function button.


Select Empty Function.


On the Configure Triggers tab, select Log Service (Log) and click Next.


Add a name and the details from Logstore and scroll down.


Add the source and target Endpoint details and click Next.


In the Configure Function Settings, set the Service and Function names, and choose the runtime. We'll add the code later.

Click Next.


Add the Service Roles and Permissions and scroll down.


Authorize Invocation Role and click Next.


The AliyunLogETLRole is required. Confirm Authorization Policy.


Now click Next.


Check the details and click Create.


Test the Log Service Function

Navigate to the code entry tab in the Function Compute function and add the following code.

# -*- coding: utf-8 -*-
import logging
import json
from aliyun.log import LogClient

def handler(event, context):
  logger = logging.getLogger()
  logger.info('start logging')
  return 'hello world'

Click Save and Invoke.


You should see the correct Result and a Status Succeeds message.

Click to see more logs.


Enable indexing by clicking Enable.


Add the details and click OK.


Once you have enabled the Logstore popup in Function Compute, you can view the log details in the same way as on Log Service page.

In Function Compute you can examine the logs one by one, most recent first.


Scroll down to see more detailed logging examples. This is a view from the Log Service.


A new log comes in on a regular basis and when triggered.

Monitoring the Log Service Function

Back in Function Compute, click through to Monitoring.


In the CloudMonitor page for Function Compute, you can see details over various time ranges of your function activity, including total invocations, average duration, error and memory details.

Click through to Request Status Details.


In the Request Status Details view, you see information at the request level.


You can also see information on billable activities.

Click Create Alarm Rule.


You can create one or more Alarm Rules for the Logstore function in Function Compute that can raise alerts on specified settings.

Add the details and scroll down.


In Notification Method you can add information on who to contact via email when an Alarm is triggered.



Firstly, we told you about Alibaba Cloud's Function Compute serverless cloud product and how you can use the service to code up multiple types of serverless applications and scenarios.

Next we told you about Alibaba Cloud's Log Service, a platform-wide offering from Alibaba Cloud that allows developers to quickly and easily set up log management tools for a wide range of products and services.

Then we told you how to hook Log Service up to Function Compute and took you through the detailed steps of how to do it before introducing the monitoring service available for all Function Compute applications.

Look out for more tutorials on various Function Compute scenarios, Cloud Monitoring scenarios, Log Service applications and scenarios and many other exciting products and services from Alibaba Cloud.

0 0 0
Share on

Alibaba Clouder

2,600 posts | 753 followers

You may also like