ALIYUN::FC::Trigger

更新时间:
复制 MD 格式

The ALIYUN::FC::Trigger type specifies how a function is triggered.

In an event-driven computing model, an event source produces events and a function handles them. A trigger provides a centralized and unified way to manage different event sources. When an event occurs, the event source invokes the function associated with the trigger if the event meets the trigger's rules.

Syntax

{
  "Type": "ALIYUN::FC::Trigger",
  "Properties": {
    "TriggerConfig": Map,
    "InvocationRole": String,
    "FunctionName": String,
    "ServiceName": String,
    "TriggerName": String,
    "TriggerType": String,
    "Qualifier": String,
    "SourceArn": String
  }
}

Properties

Property Name

Type

Required

Update allowed

Description

Constraints

ServiceName

String

Yes

No

The name of the service.

The value must be 1 to 128 characters in length.

FunctionName

String

Yes

No

The name of the function for which to create the trigger.

None

TriggerName

String

Yes

No

The name of the trigger.

The value must be 1 to 128 characters in length. It must start with a letter or an underscore (_) and can contain digits, letters, hyphens (-), and underscores (_).

TriggerType

String

Yes

No

The type of the trigger.

Valid values:

  • oss: an OSS trigger.

  • log: a Simple Log Service trigger.

  • tablestore: a Tablestore trigger.

  • timer: a time trigger.

  • mns_topic: an MNS topic trigger.

  • cdn_events: a CDN event trigger.

  • http: an HTTP trigger.

    Note

    An HTTP trigger can only be created when you create a function. It cannot exist with triggers of other types.

TriggerConfig

Map

Yes

Yes

The trigger configuration.

The trigger configuration varies based on the trigger type.

InvocationRole

String

No

Yes

The role that grants the event source the permission to run the function on your behalf. For example: acs:ram::164696547407****:role/fc-test.

This parameter is not required for time triggers and HTTP triggers. It is required for all other trigger types.

The trusted entities and policies that can be authorized for different types of trigger roles are as follows:

  • oss

    • Trusted entity: Alibaba Cloud service oss.aliyuncs.com

    • Policy: Action: fc:InvokeFunction

  • log

    • Trusted entity: Alibaba Cloud service log.aliyuncs.com

    • Policy: Action: log:PostLogStoreLogs

  • ots

    • Trusted entity: Alibaba Cloud account 1604337383174****

    • Policy: Action: fc:InvokeFunction, ots:BatchGet*, ots:Describe*, ots:Get*, ots:List*

  • mns

    • Trusted entity: Alibaba Cloud service mns.aliyuncs.com

    • Policy: Action: dm:BatchSendMail, dm:SingleSendMail, dm:Query

  • cdn

    • Trusted entity: Alibaba Cloud service cdn.aliyuncs.com

    • Policy: Action: fc:InvokeFunction

SourceArn

String

No

No

The Alibaba Cloud Resource Name (ARN) of the event source.

This parameter is not required for time triggers and HTTP triggers. It is required for all other trigger types.

The ARN formats and examples for different triggers are as follows:

  • oss

    • Format: acs:oss:<RegionId>:<TenantId>:<OssBucketName>

    • Example: acs:oss:cn-hangzhou:164696547407****:test-trigger-bucket

  • log

    • Format: acs:log:<RegionId>:<TenantId>:project/<Value of Project in LogConfig>

    • Example: acs:log:cn-hangzhou:164696547407****:project/logtrigger

  • ots

    • Format: acs:ots:<RegionId>:<TenantId>:instance/<OtsInstance>/table/<OtsTable>

    • Example: acs:ots:cn-hangzhou:164696547407****:instance/otstrigger/table/tb

  • mns

    • Format: acs:mns:<RegionId>:<TenantId>:/topics/<MnsTopic>

    • Example: acs:mns:cn-hangzhou:164696547407****:/topics/triggertopic

  • cdn

    • Format: acs:cdn:*:<TenantId>

    • Example: acs:cdn:*:164696547407****

Note

TenantId is the Alibaba Cloud account ID.

Qualifier

String

No

Yes

Trigger version.

Default value: LATEST.

oss-TriggerConfig syntax

"TriggerConfig": {
  "BucketName": String,
  "Events": List,
  "Filter": Map
}      

oss-TriggerConfig properties

Property name

Type

Required

Update allowed

Description

Constraints

BucketName

String

Yes

No

The name of the bucket.

Select a bucket in Object Storage Service (OSS) as the event source. The selection box displays buckets in the same region.

Events

String

Yes

Yes

The triggering event, which is an operation that you perform on an Alibaba Cloud resource.

Example: oss:ObjectCreated:*.

Filter

Map

No

Yes

The trigger rule, which is used to prevent trigger loops.

The values of Prefix and Suffix are custom strings.

Example:

"Filter": {
  "Key": {
    "Prefix": "prefix",
    "Suffix": "suffix"
  }
}

log-TriggerConfig syntax

"TriggerConfig": {
  "SourceConfig": Map,
  "LogConfig": Map,
  "JobConfig": Map,
  "FunctionParameter": Map,
  "Enable": Boolean
}

log-TriggerConfig properties

Property Name

Type

Required

Update allowed

Description

Constraints

SourceConfig

Map

Yes

No

The configuration of the Logstore.

Example: {"LogStore": "<SLS_LogStore>"}.

LogConfig

Map

Yes

Yes

The configuration of the log project and trigger logs.

The log project must be specified and cannot be updated. The trigger log is optional and can be updated. The trigger log cannot be the same as the Logstore specified in SourceConfig.

Example: {"Project": "<SLS_Project>", "LogStore": "SLS_LogStore"}.

JobConfig

Map

Yes

Yes

The job configuration, which is used to set the maximum number of retries and the trigger interval.

The trigger interval is measured in seconds.

The maximum number of retries can range from 0 to 100.

Example: {"MaxRetryTime": 3, "TriggerInterval": 60}.

FunctionParameter

Map

Yes

Yes

The function configuration, which is used to pass parameters to the function.

Enter braces ({}) if no parameters are specified.

Enable

Boolean

Yes

Yes

The status of the Simple Log Service trigger.

Valid values:

  • true

  • false

timer-TriggerConfig syntax

"TriggerConfig": {
  "Payload": String,
  "CronExpression": String,
  "Enabled": Boolean
}

timer-TriggerConfig properties

Property Name

Type

Required

Update allowed

Description

Constraints

Payload

String

No

Yes

The trigger message.

None

CronExpression

String

Yes

Yes

The cron expression, which is used to set the trigger time.

The cron expression runs in UTC, which is 8 hours behind UTC+8.

For more information, see CronExpression.

Enabled

Boolean

Yes

Yes

Indicates whether to enable the trigger.

Valid values:

  • true

  • false

tablestore-TriggerConfig syntax

"TriggerConfig": {
  "InstanceName": String,
  "TableName": String
}

tablestore-TriggerConfig properties

Property Name

Type

Required

Update allowed

Description

Constraints

InstanceName

String

Yes

No

The name of the Tablestore instance.

None

TableName

String

Yes

No

The name of the data table.

None

mns_topic-TriggerConfig syntax

"TriggerConfig": {
  "NotifyStrategy": String,
  "NotifyContentFormat": String,
  "FilterTag": String
}

mns_topic-TriggerConfig properties

Property Name

Type

Required

Update allowed

Description

Constraints

NotifyStrategy

String

Yes

No

The retry policy.

Valid values:

  • BACKOFF_RETRY: backoff retry.

  • EXPONENTIAL_DECAY_RETRY: exponential decay retry.

For more information, see NotifyStrategy.

NotifyContentFormat

String

Yes

No

The event format.

Valid values:

  • STREAM

  • JSON

FilterTag

String

No

No

The filter tag.

None

cdn-TriggerConfig syntax

"TriggerConfig": {
  "EventName": String,
  "EventVersion": String,
  "Notes": String,
  "Filter": List
}

cdn-TriggerConfig properties

Property Name

Type

Required

Update allowed

Description

Constraints

EventName

String

Yes

No

The triggering event.

Valid values:

  • CachedObjectsBlocked

  • CachedObjectsPushed

  • CachedObjectsRefreshed

  • LogFileCreated

  • CdnDomainStarted

  • CdnDomainStopped

  • CdnDomainAdded

  • CdnDomainDeleted

EventVersion

String

Yes

No

The version of the triggering event.

None

Notes

String

Yes

Yes

Notes.

None

Filter

List

Yes

Yes

The filter.

The key for each filter is Domain. Add at least one filter. Example: [{"Domain": "example.com"}, {"Domain": "example.org"}, ...].

http-TriggerConfig syntax

Note

An HTTP trigger can be created only when you create a function. It cannot be used with triggers of other types.

"TriggerConfig": {
  "AuthType": String,
  "Methods": List
}

http-TriggerConfig properties

Property Name

Type

Required

Update allowed

Description

Constraints

AuthType

String

Yes

No

The authentication method.

Valid values:

  • anonymous

  • function

Methods

List

Yes

No

The request methods.

Valid values:

  • GET

  • POST

  • PUT

  • DELETE

  • HEAD

  • PATCH

Example: ["GET"] or ["POST", "PUT"].

Return value

Fn::GetAtt

  • TriggerId: The unique ID of the trigger.

  • ServiceName: The name of the service.

  • FunctionName: The name of the function.

  • TriggerName: The name of the trigger.

  • UrlInternet: The public endpoint.

  • UrlIntranet: The private endpoint.

Examples

  • YAML format

    ROSTemplateFormatVersion: '2015-09-01'
    Parameters:
      ServiceName:
        Type: String
      FunctionName:
        Type: String
      CdnDomain:
        Type: Json
      SourceConfigLogStore:
        Type: String
      LogConfig:
        Type: Json
        Description: 'for example: {"Project": "testviper", "LogStore": "vipertest"}'
      MnsTopic:
        Type: String
      OssBucketName:
        Type: String
      OtsInstance:
        Type: String
      OtsTable:
        Type: String
      OtsInvocationRoleArn:
        Type: String
    Resources:
      Service:
        Type: 'ALIYUN::FC::Service'
        Properties:
          ServiceName:
            Ref: ServiceName
      Function:
        DependsOn: Service
        Type: 'ALIYUN::FC::Function'
        Properties:
          ServiceName:
            Ref: ServiceName
          Code:
            SourceCode: "def handler(event, context):\n\treturn 'Hello World!'"
          Handler: index.handler
          Runtime: python3
          FunctionName:
            Ref: FunctionName
      Role:
        Type: 'ALIYUN::RAM::Role'
        Properties:
          AssumeRolePolicyDocument:
            Version: 1
            Statement:
              - Action: 'sts:AssumeRole'
                Effect: Allow
                Principal:
                  Service:
                    - cdn.aliyuncs.com
                    - fc.aliyuncs.com
                    - mns.aliyuncs.com
                    - log.aliyuncs.com
                    - oss.aliyuncs.com
          Policies:
            - PolicyName: FcTrigger
              PolicyDocument:
                Version: '1'
                Statement:
                  - Effect: Allow
                    Action:
                      - 'fc:InvokeFunction'
                      - 'log:PostLogStoreLogs'
                      - 'dm:*'
                      - 'dm:BatchSendMail'
                      - 'dm:SingleSendMail'
                      - 'dm:Query*'
                    Resource:
                      - '*'
          RoleName: Trigger
      CdnTrigger:
        DependsOn: Function
        Type: 'ALIYUN::FC::Trigger'
        Properties:
          ServiceName:
            Ref: ServiceName
          FunctionName:
            Ref: FunctionName
          TriggerName: cdn
          TriggerType: cdn_events
          SourceArn:
            'Fn::Sub': 'acs:cdn:*:${ALIYUN::TenantId}'
          InvocationRole:
            'Fn::GetAtt':
              - Role
              - Arn
          TriggerConfig:
            EventName: CachedObjectsBlocked
            EventVersion: 1.0.0
            Notes: test
            Filter:
              Domain:
                Ref: CdnDomain
      LogTrigger:
        DependsOn: Function
        Type: 'ALIYUN::FC::Trigger'
        Properties:
          ServiceName:
            Ref: ServiceName
          FunctionName:
            Ref: FunctionName
          TriggerName: log
          TriggerType: log
          SourceArn:
            'Fn::Join':
              - ''
              - - 'Fn::Sub': 'acs:log:${ALIYUN::Region}:${ALIYUN::TenantId}:project/'
                - 'Fn::Select':
                    - Project
                    - Ref: LogConfig
          InvocationRole:
            'Fn::GetAtt':
              - Role
              - Arn
          TriggerConfig:
            SourceConfig:
              LogStore:
                Ref: SourceConfigLogStore
            LogConfig:
              Ref: LogConfig
            JobConfig:
              MaxRetryTime: 3
              TriggerInterval: 60
            FunctionParameter: {}
            Enable: false
      MnsTrigger:
        DependsOn: Function
        Type: 'ALIYUN::FC::Trigger'
        Properties:
          ServiceName:
            Ref: ServiceName
          FunctionName:
            Ref: FunctionName
          TriggerName: mns
          TriggerType: mns_topic
          SourceArn:
            'Fn::Sub': 'acs:mns:${ALIYUN::Region}:${ALIYUN::TenantId}:/topics/${MnsTopic}'
          InvocationRole:
            'Fn::GetAtt':
              - Role
              - Arn
          TriggerConfig:
            NotifyStrategy: BACKOFF_RETRY
            NotifyContentFormat: STREAM
            FilterTag: test
      OssTrigger:
        DependsOn: Function
        Type: 'ALIYUN::FC::Trigger'
        Properties:
          ServiceName:
            Ref: ServiceName
          FunctionName:
            Ref: FunctionName
          TriggerName: oss
          TriggerType: oss
          SourceArn:
            'Fn::Sub': 'acs:oss:${ALIYUN::Region}:${ALIYUN::TenantId}:${OssBucketName}'
          InvocationRole:
            'Fn::GetAtt':
              - Role
              - Arn
          TriggerConfig:
            BucketName:
              Ref: OssBucketName
            Events:
              - 'oss:ObjectCreated:*'
            Filter:
              Key:
                Prefix: b
                Suffix: a
      OtsTrigger:
        DependsOn: Function
        Type: 'ALIYUN::FC::Trigger'
        Properties:
          ServiceName:
            Ref: ServiceName
          FunctionName:
            Ref: FunctionName
          TriggerName: ots
          TriggerType: tablestore
          SourceArn:
            'Fn::Sub': >-
              acs:ots:${ALIYUN::Region}:${ALIYUN::TenantId}:instance/${OtsInstance}/table/${OtsTable}
          InvocationRole:
            Ref: OtsInvocationRoleArn
          TriggerConfig:
            InstanceName:
              Ref: OtsInstance
            TableName:
              Ref: OtsTable
      TimerTrigger:
        DependsOn: Function
        Type: 'ALIYUN::FC::Trigger'
        Properties:
          ServiceName:
            Ref: ServiceName
          FunctionName:
            Ref: FunctionName
          TriggerName: timer
          TriggerType: timer
          TriggerConfig:
            CronExpression: 0 0/5 * * * *
            Enabled: true
  • JSON format

    {
      "ROSTemplateFormatVersion": "2015-09-01",
      "Parameters": {
        "ServiceName": {
          "Type": "String"
        },
        "FunctionName": {
          "Type": "String"
        },
        "CdnDomain": {
          "Type": "Json"
        },
        "SourceConfigLogStore": {
          "Type": "String"
        },
        "LogConfig": {
          "Type": "Json",
          "Description": "for example: {\"Project\": \"testviper\", \"LogStore\": \"vipertest\"}"
        },
        "MnsTopic": {
          "Type": "String"
        },
        "OssBucketName": {
          "Type": "String"
        },
        "OtsInstance": {
          "Type": "String"
        },
        "OtsTable": {
          "Type": "String"
        },
        "OtsInvocationRoleArn": {
          "Type": "String"
        }
      },
      "Resources": {
        "Service": {
          "Type": "ALIYUN::FC::Service",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            }
          }
        },
        "Function": {
          "DependsOn": "Service",
          "Type": "ALIYUN::FC::Function",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            },
            "Code": {
              "SourceCode": "def handler(event, context):\n\treturn 'Hello World!'"
            },
            "Handler": "index.handler",
            "Runtime": "python3",
            "FunctionName": {
              "Ref": "FunctionName"
            }
          }
        },
        "Role": {
          "Type": "ALIYUN::RAM::Role",
          "Properties": {
            "AssumeRolePolicyDocument": {
              "Version": 1,
              "Statement": [
                {
                  "Action": "sts:AssumeRole",
                  "Effect": "Allow",
                  "Principal": {
                    "Service": [
                      "cdn.aliyuncs.com",
                      "fc.aliyuncs.com",
                      "mns.aliyuncs.com",
                      "log.aliyuncs.com",
                      "oss.aliyuncs.com"
                    ]
                  }
                }
              ]
            },
            "Policies": [
              {
                "PolicyName": "FcTrigger",
                "PolicyDocument": {
                  "Version": "1",
                  "Statement": [
                    {
                      "Effect": "Allow",
                      "Action": [
                        "fc:InvokeFunction",
                        "log:PostLogStoreLogs",
                        "dm:*",
                        "dm:BatchSendMail",
                        "dm:SingleSendMail",
                        "dm:Query*"
                      ],
                      "Resource": [
                        "*"
                      ]
                    }
                  ]
                }
              }
            ],
            "RoleName": "Trigger"
          }
        },
        "CdnTrigger": {
          "DependsOn": "Function",
          "Type": "ALIYUN::FC::Trigger",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            },
            "FunctionName": {
              "Ref": "FunctionName"
            },
            "TriggerName": "cdn",
            "TriggerType": "cdn_events",
            "SourceArn": {
              "Fn::Sub": "acs:cdn:*:${ALIYUN::TenantId}"
            },
            "InvocationRole": {
              "Fn::GetAtt": [
                "Role",
                "Arn"
              ]
            },
            "TriggerConfig": {
              "EventName": "CachedObjectsBlocked",
              "EventVersion": "1.0.0",
              "Notes": "test",
              "Filter": {
                "Domain": {
                  "Ref": "CdnDomain"
                }
              }
            }
          }
        },
        "LogTrigger": {
          "DependsOn": "Function",
          "Type": "ALIYUN::FC::Trigger",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            },
            "FunctionName": {
              "Ref": "FunctionName"
            },
            "TriggerName": "log",
            "TriggerType": "log",
            "SourceArn": {
              "Fn::Join": [
                "",
                [
                  {
                    "Fn::Sub": "acs:log:${ALIYUN::Region}:${ALIYUN::TenantId}:project/"
                  },
                  {
                    "Fn::Select": [
                      "Project",
                      {
                        "Ref": "LogConfig"
                      }
                    ]
                  }
                ]
              ]
            },
            "InvocationRole": {
              "Fn::GetAtt": [
                "Role",
                "Arn"
              ]
            },
            "TriggerConfig": {
              "SourceConfig": {
                "LogStore": {
                  "Ref": "SourceConfigLogStore"
                }
              },
              "LogConfig": {
                "Ref": "LogConfig"
              },
              "JobConfig": {
                "MaxRetryTime": 3,
                "TriggerInterval": 60
              },
              "FunctionParameter": {},
              "Enable": false
            }
          }
        },
        "MnsTrigger": {
          "DependsOn": "Function",
          "Type": "ALIYUN::FC::Trigger",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            },
            "FunctionName": {
              "Ref": "FunctionName"
            },
            "TriggerName": "mns",
            "TriggerType": "mns_topic",
            "SourceArn": {
              "Fn::Sub": "acs:mns:${ALIYUN::Region}:${ALIYUN::TenantId}:/topics/${MnsTopic}"
            },
            "InvocationRole": {
              "Fn::GetAtt": [
                "Role",
                "Arn"
              ]
            },
            "TriggerConfig": {
              "NotifyStrategy": "BACKOFF_RETRY",
              "NotifyContentFormat": "STREAM",
              "FilterTag": "test"
            }
          }
        },
        "OssTrigger": {
          "DependsOn": "Function",
          "Type": "ALIYUN::FC::Trigger",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            },
            "FunctionName": {
              "Ref": "FunctionName"
            },
            "TriggerName": "oss",
            "TriggerType": "oss",
            "SourceArn": {
              "Fn::Sub": "acs:oss:${ALIYUN::Region}:${ALIYUN::TenantId}:${OssBucketName}"
            },
            "InvocationRole": {
              "Fn::GetAtt": [
                "Role",
                "Arn"
              ]
            },
            "TriggerConfig": {
              "BucketName": {
                "Ref": "OssBucketName"
              },
              "Events": [
                "oss:ObjectCreated:*"
              ],
              "Filter": {
                "Key": {
                  "Prefix": "b",
                  "Suffix": "a"
                }
              }
            }
          }
        },
        "OtsTrigger": {
          "DependsOn": "Function",
          "Type": "ALIYUN::FC::Trigger",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            },
            "FunctionName": {
              "Ref": "FunctionName"
            },
            "TriggerName": "ots",
            "TriggerType": "tablestore",
            "SourceArn": {
              "Fn::Sub": "acs:ots:${ALIYUN::Region}:${ALIYUN::TenantId}:instance/${OtsInstance}/table/${OtsTable}"
            },
            "InvocationRole": {
              "Ref": "OtsInvocationRoleArn"
            },
            "TriggerConfig": {
              "InstanceName": {
                "Ref": "OtsInstance"
              },
              "TableName": {
                "Ref": "OtsTable"
              }
            }
          }
        },
        "TimerTrigger": {
          "DependsOn": "Function",
          "Type": "ALIYUN::FC::Trigger",
          "Properties": {
            "ServiceName": {
              "Ref": "ServiceName"
            },
            "FunctionName": {
              "Ref": "FunctionName"
            },
            "TriggerName": "timer",
            "TriggerType": "timer",
            "TriggerConfig": {
              "CronExpression": "0 0/5 * * * *",
              "Enabled": true
            }
          }
        }
      }
    }