Use Fun and Function Compute Together with the OSS Trigger
By Sun Feiyu
This tutorial overviews how you can use Function Compute with the OSS trigger and how you can use Fun to configure and deploy the OSS trigger. But before we get into it, let’s take a look at several important concepts mentioned in this article and also discuss some things you’ll need to complete before starting the procedure outlined in this tutorial.
First, you may want to know a thing or two about Function Compute. Function Compute is an event-driven computing service that only requires users to write and upload code. Function Compute prepares and auto-scales to the correct amount of computing resources to run user code. The user only pays for the resources required to run their code.
Next, Fun is a fun tool that we’re going to be using in this tutorial. To be more specific, it’s a tool that supports serverless deployment and allows easy management of resources such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the
In this next section, consider the following example that shows how Fun uses an Object Storage Service (OSS) trigger to associate OSS with Function Compute.
Event source service
Event source services are a collection of services that can trigger functions, such as OSS, Log Service, API Gateway, Timer, and HTTP requests. When an event occurs, the corresponding event source determines whether to invoke functions. For example, after you create an OSS PutObject trigger, uploading an object to a specified OSS Bucket is an event and will trigger the corresponding function. In this process, the OSS is an event source service. Function Compute supports a wide range of event sources to meet your customized requirements.
Use Fun to Configure and Deploy the OSS trigger
As an overview of what will happen in this tutorial, a Function Compute trigger describes a set of rules. When an event meets these rules, the event source will trigger the corresponding function. Alibaba Cloud OSS is seamlessly integrated with Function Compute. You can set functions to process various types of events. When OSS detects an event of the specified type, it automatically calls the corresponding function to process it. Next, you can use Fun to develop, build, and deploy resources by describing specified resources in the
template.yml file. Y
To do this, you need to describe the OSS trigger in the
yml file by following the specifications:
Type: OSS # trigger type
BucketName: coco-superme # oss bucket name
Below is the description of the Trigger parameters:
oss-trigger-name: the name of a user-defined trigger. The name must be unique.
Type: indicates the type of the event source. In this example, the event type described in the YML file is OSS.
BucketName: OSS bucket name.
Events: the type of an OSS event.
Filter: the parameter for filtering OSS objects. Only objects that meet the filter conditions trigger corresponding functions. The filter contains the following attributes:
Key: A filter can filter object keys, which contain the following attributes:
Prefix: matching prefix
Suffix: matching suffix
If a file stored in OSS begins with
source/ and ends with
.png, OSS will automatically invoke the corresponding function to process the file when it is triggered by a user-defined event.
You can run
fun deploy to create, configure and deploy related services:
using region: cn-shanghai
using accountId: ***********8320
using accessKeyId: ***********mTN4
using timeout: 10Waiting for service oss-test-service to be deployed...
Waiting for function oss-test-function to be deployed...
Waiting for packaging function oss-test-function code...
package function oss-test-function code done
Waiting for OSS trigger oss-trigger-name to be deployed...
function oss-trigger-name deploy success
function oss-test-function deploy success
service oss-test-service deploy successPS D:\fun\examples\oss-trigger>
You can view the OSS trigger that you have configured in the Function Compute console, as shown in the following figure:
The OSS trigger
oss-trigger-name has been created. As you can see, Fun is a really useful tool.
If you see the following log during the deployment:
The name of the OSS trigger configured in the Function Compute console does not match the local configuration. This means that the collection of triggers configured in the Function Compute console do not fall within the scope of the triggers locally configured. Fun will not modify this trigger. You can remove this trigger manually through the Function Compute console if necessary.
Make sure to avoid cyclic triggering when you use an OSS trigger. A typical scenario of cyclic triggering is that uploading a file from an OSS Bucket trigger a function, which then generates one or more files. New files are then written back to the original OSS bucket and the write operation triggers the function again, leading to an execution loop.
The aforementioned process is similar to an endless recursion. To avoid unnecessary costs incurred by cyclic function execution, we strongly recommend that you add a prefix/suffix. For example, you can add the
src/ prefix to the directory of the Bucket that triggers the function and the
dst/ prefix to the directory where newly-generated files are written. In this way, newly-generated files will not trigger the function again.
Below are some points that are relevant to the things we learned in this blog:
- You must specify all the required attributes when defining an OSS trigger in the yml file.
- When OSS detects a specific event, the event information will be encoded as a JSON string and passed to the corresponding event processing function. You can view the event information in the code. Related information is not within the scope of this article.
- Make sure that the configuration is correct and you can focus on writing the function logic. Function Compute can process large volumes of data in real time and in parallel.