IBM / ibm-cloud-functions-polyglot-development

Simple demo showing how to write actions in different languages with IBM Cloud Functions. Created for the "Build a cloud native app with Apache OpenWhisk" webinar.
Apache License 2.0
2 stars 7 forks source link
docker javascript openwhisk openwhisk-building-block openwhisk-getting-started serverless

Polyglot IBM Cloud Functions with JavaScript and Docker

Note: This demo application was built in 2016. The concepts are the same, but there are alternative approaches for binding Cloudant to the Cloud Functions environment and another way to use Docker that doesn't require you to store action code in the public Docker Hub.

Simple demo showing IBM Cloud Functions (powered by Apache OpenWhisk) programming with actions, triggers, and rules. Created for the Build a cloud native app with Apache OpenWhisk webinar.

This example shows how to write actions in both JavaScript and Docker, respond to database triggers, and invoke actions in a workflow asynchronously.

When an image is added to a Cloudant database, this event triggers the JavaScript action to download the file and resize it. It also invokes an optical character recognition (OCR) library packaged as an action in Docker.

High level diagram

  1. Image uploaded to Cloudant
  2. Trigger fired in IBM Cloud Functions
  3. JavaScript action parses image metadata and transforms image
  4. Docker action parses image with OCR
  5. Extracted info stored back to Cloudant

JavaScript Action process-check.js

This action is invoked by the new-check-deposit trigger, which fires when an image of a check is added to a Cloudant database (named incoming-checks). The action downloads the image, invokes the parse-image.sh action to extract account information, resizes the image, then stores a copy back in another Cloudant database (named processed-checks) along with the account data.

Docker Action parse-image.sh

This action is invoked by the process-check.js action above. The action downloads the original check image, parses it for account and routing number data, then returns that to the calling action which in turn stores it in the database.

Running the demo

There is a convenience script for creating and destroying the actions, triggers, and rules. It retrieves configuration information from a local.env file that is created from template.local.env.

After you've installed the wsk binary, created a Cloudant service instance, and installed Docker for your platform, the script can then be used to set up, tear down, and inspect the current configuration:

./deploy.sh --install
./deploy.sh --uninstall
./deploy.sh --env

Install the wsk CLI from Bluemix

After registering for the IBM Cloud, navigate to the "Functions" section. You'll find it in the left navigation, under the three horizontal bar (hamburger) icon.

Click the "Download CLI" button and place the wsk binary in your path, such as in ~/bin. Open a terminal and set your namespace and authorization as shown in step 2. Then create your first action in step 3 to validate your setup.

Set up Cloudant database in the IBM Cloud

Under the "Services" option in the left navigation, choose "Data & Analytics", then view the Services portfolio and provision an instance of Cloudant. Save the "Service Credentials" information to local.env and launch the dashboard.

In the Cloudant dashboard, create two databases: incoming-checks and processed-checks.

Install Docker for Mac (or your platform of choice)

The deploy.sh script assumes that you have Docker in your path and that you have a Docker Hub account. Install Docker for your platform and sign up for a Docker Hub account. Enter the credentials in local.env.