Triggers can cause functions to run. In an event-driven computing model, the event source is the event producer, the function is the event handler, and the trigger manages different event sources in a centralized manner. When an event that matches the trigger-defined rules occurs, the event source automatically invokes the corresponding function.
What is a trigger?
Function Compute provides an event-driven computing model. Function execution is driven by events. Functions can be invoked in the Function Compute console, or by using the fcli command-line tool, Aliyun Serverless VSCode Extension, the SDK, or event sources. You can create a trigger in a specified function. The trigger describes a set of rules. When an event meets these rules, the event source triggers a corresponding function.
- Example 1: Image status changes in Object Storage Service (OSS) trigger function execution.
If an application uses OSS to store images that you upload, you can invoke functions to download and process these images, and then store the results in OSS or other services. If OSS can detect newly uploaded images and automatically invoke the corresponding functions, you do not have to invoke functions manually. This simplifies the development and use. OSS event triggers detect these events and invoke functions in Function Compute. After you configure an OSS event trigger, it automatically invokes functions to download and process newly uploaded images.
- Example 2: Log updates in Log Service (SLS) trigger function execution.
If an application uses SLS to regularly collect new log data, you can invoke functions to query and analyze incremental logs. If SLS can detect log updates and automatically invoke the corresponding functions, you do not have to invoke functions manually. SLS triggers detect these events and invoke functions in Function Compute. After you configure an SLS trigger, it automatically invokes functions to consume incremental data when logs are updated.
- Example 3: Function execution is triggered at a specified time interval.
If an application needs to collect data at one-hour intervals, you can invoke functions every one hour to collect and process data. If functions in Function Compute can automatically run every one hour, you do not have to remember to invoke functions manually at the scheduled time. Time triggers detect time events and invoke functions in Function Compute. After you configure a time trigger, it automatically invokes functions to collect and process data at a specified time interval.
In addition to the preceding scenarios, Function Compute supports many other event sources. For more information, see Triggers. Other event sources will be integrated successively.
- triggerName: the name of the trigger.
- triggerType: the type of the trigger, such as the OSS event trigger, HTTP trigger, or time trigger.
- sourceArn: the Alibaba Cloud Resource Name (ARN) that is used to trigger function execution.
- If you use a trigger (such as an OSS event trigger or an SLS trigger) that involves another Alibaba Cloud service to invoke a function in Function Compute, this parameter is required.
- If you use a trigger (such as a time trigger or an HTTP trigger) that does not involve any other Alibaba Cloud services to invoke a function in Function Compute, this parameter is not required.
For example, the value of the sourceArn parameter for an OSS event trigger is as follows:
- invocationRole: the role used by the event source to invoke the function. The role grants the event source the permission to invoke the function. For more information about roles of event sources, see User permissions.
- qualifier: the version or alias of the triggered service. For more information about versions and aliases, see Introduction.
- triggerConfig: the configuration of the trigger. For more information about the configurations of different triggers, see the corresponding documents.