Git Product home page Git Product logo

ibm-cloud-functions-action-trigger-rule's Introduction

IBM Cloud Functions - Your first Action, Trigger, and Rule

Read this in other languages: 한국어.

Simple demo showing Apache OpenWhisk actions, triggers, and rules on the IBM Cloud Functions platform. Created for the Build a cloud native app with Apache OpenWhisk webinar. First, you'll need an IBM Cloud account and the latest OpenWhisk command line tool.

High level diagram

Action: handler.js

This simple JavaScript function (called an action in OpenWhisk) accepts a params argument and writes information that can be retrieved from the Cloud Functions activation log and/or the IBM Cloud Functions monitoring console. It also returns a JSON object with the current date.

First, open a terminal window to start polling the activation log. The console.log statements in the action will be logged here, which you can stream with the following command:

ibmcloud fn activation poll

In another terminal window, upload the action file as a Cloud Function using the following command:

ibmcloud fn action create handler handler.js

Then invoke it manually. This will echo the resulting JSON message to the current window and log the activation in the other window.

ibmcloud fn action invoke --blocking handler

Trigger: every-20-seconds

This trigger uses the built-in alarm package feed to fire events every 20 seconds. This is specified through cron syntax in the cron parameter. The maxTriggers parameter ensures that it only fires for five minutes (15 times), rather than indefinitely.

Create it with the following command:

ibmcloud fn trigger create every-20-seconds \
  --feed  /whisk.system/alarms/alarm \
  --param cron '*/20 * * * * *' \
  --param maxTriggers 15

Rule: invoke-periodically

This rule shows how the every-20-seconds trigger can be declaratively mapped to the handler.js action. Notice that it's named somewhat abstractly so that if we wanted to use a different trigger - perhaps something that fires every minute instead - we could still keep the logical name.

Create the rule with the following command:

ibmcloud fn rule create \
  invoke-periodically \
  every-20-seconds \
  handler

At this point you can check the activation log that you are tailing in the other window to confirm that the action is invoked by the trigger.

Installation instructions

Cloud Functions developers will often automate the creation of actions, triggers, and rules as they iterate on their application. The convention that has arisen in many sample apps is to use a deploy.sh script, often in conjunction with a local.env file to externalize environment variables.

The script can be used to set up, tear down, and see the current configuration:

./deploy.sh --install
./deploy.sh --uninstall
./deploy.sh --env # Not used in this demo

Note: deploy.sh will be replaced with wskdeploy in the future. wskdeploy uses a manifest to deploy declared triggers, actions, and rules to OpenWhisk.

Troubleshooting

Check for errors first in the activation log. Tail the log on the command line with ibmcloud fn activation poll or drill into details visually with the Cloud Functions monitoring console.

If the error is not immediately obvious, make sure you have the latest version of the wsk CLI installed. If it's older than a few weeks, download an update.

ibmcloud fn property get --cliversion

License

Apache 2.0

ibm-cloud-functions-action-trigger-rule's People

Contributors

animeshsingh avatar fabriziocucci avatar hongjsk avatar imgbotapp avatar jzaccone avatar krook avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.