Guidelines for Function Compute Development -- OSS Trigger
Terms and descriptions:
Function Compute: Alibaba Cloud Function Compute is an event-driven computing service that dynamically allocates resources after you write and upload code. After Function Compute allocates required computing resources, it elastically runs your code. Fees are calculated based on the actual resources consumed. For more information, see https://www.alibabacloud.com/help/product/50980.htm.
Fun: Fun is a developer tool for serverless applications. It helps you manage 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 template.yml file. For more information, see https://github.com/aliyun/fun） .
The following example shows how Fun uses an Object Storage Service (OSS) trigger to associate OSS with Function Compute.
Event sources are the collection of services that can invoke functions. Event sources include services such as OSS, Log Service, API Gateway, timers, 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 considered to be an event. The corresponding functions are then invoked. During this process, OSS is the event source. Function Compute supports a wide range of event sources to meet your requirements. For more information, see https://yq.aliyun.com/articles/679488.
A Function Compute trigger describes a set of rules. When an event matches the rules, the event source invokes the corresponding functions. Alibaba Cloud OSS is seamlessly integrated with Function Compute. You can configure functions for different types of events. When OSS detects a specified type of event, functions are automatically invoked.
You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file. You must describe an OSS trigger in the YML file by following the specifications. For example:
Events: oss-trigger-name: Type: OSS # trigger type Properties: BucketName: coco-superme # oss bucket name Events: - oss:ObjectCreated:* - oss:ObjectRemoved:DeleteObject Filter: Key: Prefix: source/ Suffix: .png
Filter: Filters parameters for OSS objects. Only objects that meet the filter conditions can invoke functions. The filter contains the following attributes:
Key: The filter can filter object keys and contains the following attributes:
If a file stored in OSS begins with
source/ and ends with
.png, then OSS automatically invokes functions to process the file when a specified event is detected. Sample code.
You can run the
fun deploy command to create and deploy services:
using region: cn-shanghai using accountId: ***********8320 using accessKeyId: ***********mTN4 using timeout: 10 Waiting 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 success PS 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.
The following log entry may appear when you deploy services:
This indicates that the name of the OSS trigger configured in the Function Compute console is inconsistent with the one described in the YML file. This means that the triggers configured in the Function Compute console are inconsistent with those you have described in the YML file. Fun will not modify the trigger configured in the console. You can delete the trigger in the console, or manually invoke functions when necessary.
When you use OSS triggers, you must avoid recursive calls. For example, in a scenario where files are uploaded to an OSS bucket, the upload operation invokes a function. This function then generates one or multiple files that are written to the OSS bucket. The write operation invokes the function again, which leads to an execution loop.
To avoid costs incurred by recursive calls as described in the preceding example, we recommend that you add a prefix and suffix to the directories, respectively. For example, you can add the suffix
src/ to the bucket directory and
dst/ to the directly where files generated by the function are stored. This prevents the function from being invoked again by the generated files.