Guidelines for Function Compute Development -- OSS Trigger

  1. 云栖社区>
  2. 阿里云 Serverless Computing>
  3. 博客>
  4. 正文

Guidelines for Function Compute Development -- OSS Trigger

tanhe123 2019-05-16 20:01:17 浏览449

Preface

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

image

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.

Configure Fun and OSS trigger

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
  • oss-trigger-name: The name of the trigger. The name must be unique.
  • Type: Indicates the type of the event source. For more information about supported event sources, see https://github.com/aliyun/fun/blob/master/docs/specs/2018-04-03.md#OSS. In this example, the event type described in the YML file is OSS.
  • BucketName: The name of the OSS bucket. For more information about creating OSS buckets, see https://help.aliyun.com/document_detail/74762.html ).
  • Events: Indicates the type of the OSS event.
  • 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:

      • Prefix: Matches prefixes.
      • Suffix: Matches suffixes.

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:

image

The OSS trigger oss-trigger-name has been created.

Notes:

Configuration errors

The following log entry may appear when you deploy services:

image

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.

Recursive calls

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.

Summary

  • OSS triggers are described in the YML file. You must specify all attributes.
  • When OSS detects a specific event, the event information will be encrypted into a JSON string and passed to the corresponding function for processing. You can view the event information in the code. For more information, see https://www.alibabacloud.com/help/doc-detail/73331.htm .
  • 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.