In this tutorial, we will show you how to create an Alibaba Cloud Log Service project which triggers an Alibaba Cloud Function Compute function.
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.
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.
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.
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 =
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.
Add the Service Roles and Permissions and scroll down.
Authorize Invocation Role and click Next.
AliyunLogETLRole is required. Confirm Authorization Policy.
Now click Next.
Check the details and click Create.
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.
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.
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.
Alibaba Clouder - June 16, 2020
Alibaba Clouder - March 19, 2019
Alibaba Clouder - November 8, 2019
Alibaba Clouder - April 3, 2019
Alibaba Clouder - April 15, 2019
Alibaba Clouder - June 2, 2020
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
An all-in-one service for log-type dataLearn More
API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.Learn More
More Posts by Alibaba Clouder