IBM Cloud Docs
Creating triggers for events

Creating triggers for events

IBM Cloud® Functions is deprecated. Existing Functions entities such as actions, triggers, or sequences will continue to run, but as of 28 December 2023, you can’t create new Functions entities. Existing Functions entities are supported until October 2024. Any Functions entities that still exist on that date will be deleted. For more information, see Deprecation overview.

In IBM Cloud® Functions, a trigger is a declaration that you want to react to a certain type of event, whether from a user or by an event source.

The following are examples of triggers.

  • Location update events
  • Document uploads to a website.
  • Incoming email

Creating triggers from the CLI

  1. Create the trigger. Triggers must be created directly within a namespace and can't be created inside packages.

    ibmcloud fn trigger create TRIGGER_NAME
    

    The following example shows possible output from the previous command.

    ok: created trigger TRIGGER_NAME
    
  2. Verify that the trigger is created.

    ibmcloud fn trigger list
    

    The following example shows possible output from the previous command.

    triggers
    /<namespace_ID>/<TRIGGER_NAME>                            private
    

Next, you can test the trigger or create a rule to associate the trigger with an action.

Difference between feed and trigger

Feeds and triggers are closely related, but technically distinct concepts.

  • Cloud Functions processes events that flow into the system.

  • A trigger is a name for a class of events. Each event belongs to exactly one trigger; by analogy, a trigger resembles a topic in topic-based pub-sub systems. A rule is used to indicate that whenever an event from trigger arrives, invoke action with the trigger payload.

  • A feed is a convenient way to configure an external event source to fire trigger events that can be consumed by Cloud Functions. A feed is a stream of events that all belong to some trigger. Pre-installed packages, installable packages, and your own custom packages might contain feeds. A feed is controlled by a feed action, which handles creating, deleting, pausing, and resuming the stream of events that comprise a feed. The feed action typically interacts with external services that produce the events, by using a REST API that manages notifications.

Examples of feeds:

  • An IBM Cloudant data change feed that fires a trigger event each time a document in a database is added or modified.
  • A Git feed that fires a trigger event for every commit to a Git repository.

Creating a trigger for a feed

This example shows how to use a feed in the Alarms package to fire a trigger once a minute, and how to use a rule to invoke an action once a minute.

  1. Get a description list of the entities in the /whisk.system/alarms package.

    ibmcloud fn package get --summary /whisk.system/alarms
    

    The following example shows possible output from the previous command.

    package /whisk.system/alarms
        feed   /whisk.system/alarms/alarm
    
  2. Get a description of the feed in the /whisk.system/alarms package to see the parameters that you can use.

    ibmcloud fn action get --summary /whisk.system/alarms/alarm
    

    The following example shows possible output from the previous command.

    action /whisk.system/alarms/alarm: Fire trigger when alarm occurs
        (params: cron trigger_payload)
    

    The /whisk.system/alarms/alarm feed takes two parameters:

    • cron: A crontab specification of when to fire the trigger.
    • trigger_payload: The payload parameter value to set in each trigger event.
  3. Create a trigger that fires every minute.

    ibmcloud fn trigger create everyOneMinute --feed /whisk.system/alarms/alarm -p cron "* * * * *" -p trigger_payload "{\"name\":\"Mork\", \"place\":\"Ork\"}"
    

    The following example shows possible output from the previous command.

    ok: created trigger feed everyOneMinute
    
  4. Create an app. Example hello.js:

    function main(params) {
        return {payload:  'Hello, ' + params.name + ' from ' + params.place};
    }
    
  5. Create an action.

    ibmcloud fn action create hello hello.js
    
  6. Create a rule that invokes the hello action every time the everyOneMinute trigger fires.

    ibmcloud fn rule create myRule everyOneMinute hello
    

    The following example shows possible output from the previous command.

    ok: created rule myRule
    
  7. Check that the action is being invoked by polling for activation logs.

    ibmcloud fn activation poll
    

    You can see that the activations occur every minute for the trigger, the rule, and the action. The action receives the parameters {"name":"Mork", "place":"Ork"} on every invocation.