All Products
Document Center

Create a trigger

Last Updated: May 30, 2019

Triggers are used to call functions. In an event-driven computing model, the event source is the event raiser, the function is the event handler, and the trigger centrally manages different event sources. When an event that matches the trigger-defined rules occurs, the event source calls the function corresponding to the trigger.


In some cases, you may not want to run a function by directly calling it. For example, if an application is using Object Storage Service (OSS) for storing the images that you upload, you can call a function to download and process the images, and then record the results in OSS or other services. If OSS can detect newly uploaded images and automatically call the corresponding functions, you do not need to call functions manually. In this way, the development and application processes are simplified.

Function Compute provides an event-driven computing model. Function execution can be triggered in the Function Compute console, the command-line tool fcli, the SDK, or other event sources. A Function Compute trigger describes a set of rules. When an event meets these rules, the event source calls a corresponding function. Currently, Function Compute supports the following event sources.


  • triggerName: Indicates the name of the trigger.
  • triggerType: Indicates the type of the trigger, such as the OSS trigger, Time trigger, and HTTP trigger.
  • sourceArn: Indicates the event source that triggers a function execution. This property is required when other services of Alibaba Cloud are involved in calling functions of Function Compute, such as the OSS trigger and Log Service trigger. If the Time trigger, HTTP trigger, or other triggers are used, and no other Alibaba Cloud service is involved, this property is not required. For example, the value format of the OSS trigger, is acs:oss:region:accountId:bucketName.
  • invocationRole: Indicates the role used by the event source to call the function. The role grants event source the permission to call functions. For more information about roles of event sources, see Introduction.
  • qualifier:Optional. Indicates service version or alias. For more information about qualifier usage, see version management.
  • triggerConfig: Specifies the configurations of the trigger. For more information about the configurations of different triggers, see the corresponding documents.


The following example shows how to create an OSS trigger. When an OSS bucket detects that a new image is uploaded and the image is prefixed with source/, a function is called to run the resize operation. The image generated subsequently is saved to the /processed directory.


An OSS bucket for storing images is available. This bucket has the /source directory, under which the image 1.jpg used for debugging is stored.

Create function and write code

For more information about creating a function, see Basic operations. The following provides an example code. The Python runtime environments of Function Compute provide an image processing library named wand. Therefore, no third-party dependencies need to be uploaded.

  1. # -*- coding: utf-8 -*-
  2. import oss2, json
  3. from wand.image import Image
  4. def resize(event, context):
  5. evt = json.loads(event)
  6. creds = context.credentials
  7. # Required by OSS SDK
  8. auth=oss2.StsAuth(
  9. creds.access_key_id,
  10. creds.access_key_secret,
  11. creds.security_token)
  12. evt = evt['events'][0]
  13. bucket_name = evt['oss']['bucket']['name']
  14. endpoint = 'oss-' + evt['region'] + ''
  15. bucket = oss2.Bucket(auth, endpoint, bucket_name)
  16. objectName = evt['oss']['object']['key']
  17. # Processed images are saved to processed/
  18. newKey = objectName.replace("source/", "processed/")
  19. remote_stream = bucket.get_object(objectName)
  20. if not remote_stream:
  21. return
  22. remote_stream =
  23. with Image(blob=remote_stream) as img:
  24. with img.clone() as i:
  25. i.resize(128, 128)
  26. new_blob = i.make_blob()
  27. bucket.put_object(newKey, new_blob)

Create trigger

  1. Log on to the Function Compute console.

  2. See Basic operations to create a trigger.

    Create a trigger

Debug trigger

Before testing a trigger, you can debug it by triggering an event source. First, you can create a source directory under the OSS bucket and upload the image 1.jpg to this directory. Then, set an event source to call a function.

  1. On the code execution page, click Trigger.

  2. In the Test Event dialog box, select OSS Template, and edit your arn, name, ownerIdentity, and key parameters in the event template accordingly.

    1. {
    2. "events": [
    3. {
    4. "eventName": "ObjectCreated:PutObject",
    5. "eventSource": "acs:oss",
    6. "eventTime": "2017-04-21T12:46:37.000Z",
    7. "eventVersion": "1.0",
    8. "oss": {
    9. "bucket": {
    10. "arn": "acs:oss:cn-shanghai:1237050315505689:bucketname",
    11. "name": "bucketname",
    12. "ownerIdentity": "1237050315505689",
    13. "virtualBucket": ""
    14. },
    15. "object": {
    16. "deltaSize": 122539,
    17. "eTag": "688A7BF4F233DC9C88A80BF985AB7329",
    18. "key": "image/a.jpg",
    19. "size": 122539
    20. },
    21. "ossSchemaVersion": "1.0",
    22. "ruleId": "9adac8e253828f4f7c0466d941fa3db81161e853"
    23. },
    24. "region": "cn-shanghai",
    25. "requestParameters": {
    26. "sourceIPAddress": ""
    27. },
    28. "responseElements": {
    29. "requestId": "58F9FF2D3DF792092E12044C"
    30. },
    31. "userIdentity": {
    32. "principalId": "262561392693583141"
    33. }
    34. }
    35. ]
    36. }
  3. Click Invoke. A directory named processed is created under the OSS bucket and the processed image 1.jpg is stored in this directory. This process simulates how a function is executed in Function Compute when an object is created in the /source directory of the OSS bucket.

Test trigger

In the OSS console, upload an image to the /source directory of the OSS bucket configured for the trigger. A resized image with the same name is generated in the /processed directory.