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.

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.

Prerequisites

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.

Image for post
Image for post

Click Create Project.

Image for post
Image for post

Enter a Project Name and a Region and click Confirm.

Image for post
Image for post

In the popup, click Create to create a Logstore.

Image for post
Image for post

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.

Image for post
Image for post

We now have a Logstore.

Click Data Import Wizard.

Image for post
Image for post

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.

Image for post
Image for post

Let’s go back to the Logstore.

Image for post
Image for post

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

Click Create and follow the steps as before.

Image for post
Image for post

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

Image for post
Image for post

Create a Function Compute Function

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

Image for post
Image for post

In the Service page, click the Services add button.

Image for post
Image for post

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

Image for post
Image for post

In Log Configs, set Log Project and LogStore.

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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

Select Empty Function.

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

Add the source and target Endpoint details and click Next.

Image for post
Image for post

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

Click Next.

Image for post
Image for post

Add the Service Roles and Permissions and scroll down.

Image for post
Image for post

Authorize Invocation Role and click Next.

Image for post
Image for post

The AliyunLogETLRole is required. Confirm Authorization Policy.

Image for post
Image for post

Now click Next.

Image for post
Image for post

Check the details and click Create.

Image for post
Image for post

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')
logger.info(event.decode().encode())
return 'hello world'

Click Save and Invoke.

Image for post
Image for post

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

Click to see more logs.

Image for post
Image for post

Enable indexing by clicking Enable.

Image for post
Image for post

Add the details and click OK.

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

You can also see information on billable activities.

Click Create Alarm Rule.

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

Summary

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.

Reference:https://www.alibabacloud.com/blog/using-log-service-with-alibaba-clouds-function-compute_594781?spm=a2c41.12860857.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store