Functions are connected to event sources in OpenWhisk using triggers and rules. Triggers create a named event stream within the system. Triggers can be fired manually or connected to external data sources, like databases or message queues.
Rules set up a binding between triggers and serverless functions. With an active rule, each time a trigger is fired, the function will be executed with the trigger payload.
Event binding using triggers and rules for functions can be configured through the
functions: my_function: handler: index.main events: - trigger: my_trigger
This configuration will create a trigger called
servicename-my_trigger with an active rule binding
my_function to this event stream.
Rule names default to the following format
servicename-trigger-to-action. These names can be explicitly set through configuration.
functions: my_function: handler: index.main events: - trigger: name: 'my_trigger' rule: 'rule_name'
Triggers can be defined as separate resources in the
serverless.yaml file. This allows you to set up trigger properties like default parameters.
functions: my_function: handler: index.main events: - trigger: my_trigger resources: triggers: my_trigger: parameters: hello: world
This example demonstrates setting up a trigger which uses the
/whisk.system/alarms/alarm feed. The
alarm feed will fire a trigger according to a user-supplied cron schedule.
resources: triggers: alarm_trigger: parameters: hello: world feed: /whisk.system/alarms/alarm feed_parameters: cron: '*/8 * * * * *'