How to develop functions-as-a-service with Apache OpenWhisk

Apache OpenWhisk is a serverless, open source cloud platform that allows you to execute code in response to events at any scale. Apache OpenWhisk offers developers a straightforward programming model based on four concepts: actions, packages, triggers, and rules.

Actions are stateless code snippets that run on the Apache OpenWhisk platform. You can develop an action (or function) via JavaScript, Swift, Python, PHP, Java, or any binary-compatible executable, including Go programs and custom executables packaged as Linux containers. Actions can be explicitly invoked or run in response to an event. In either case, each run of an action results in an activation record that is identified by a unique activation ID. The input to an action and the result of an action are a dictionary of key-value pairs, where the key is a string and the value a valid JSON value.

Packages provide event feeds; anyone can create a new package for others to use.

Triggers associated with those feeds fire when an event occurs, and developers can map actions (or functions) to triggers using rules.

The following commands are used to create, update, delete, and list an action in Apache OpenWhisk:

Usage: wsk action [command]

Available Commands: create create a new action update update an existing action, or create an action if it does not exist invoke invoke action get get action delete delete action list list all actions in a namespace or actions contained in a package

Set up OpenWhisk

Let’s explore how that works in action. First, download Minishift to create a single-node local OKD (community distribution of Kubernetes that powers Red Hat OpenShift) cluster on your workstation:

$ minishift start --vm-driver=virtualbox --openshift-version=v3.10.0

Once Minishift is up and running, you can log in with admin /admin and create a new project (namespace). The project OpenWhisk on OpenShift provides the OpenShift templates required to deploy Apache OpenWhisk:

Apache OpenWhisk is comprised of many components that must start up and sync with each other, and this process can take several minutes to stabilize. The following command will wait until the component pods are running:

Develop a simple Java Action

Maven archetype is a Maven project templating toolkit. In order to create a sample Java Action project, you won’t refer to central Maven archetype, but you need to generate your own Maven archetype first as below:

Having created the function hello-openwhisk, verify the function by invoking it:

$ wsk -i action invoke hello-openwhisk --result

As all the OpenWhisk actions are asynchronous, you need to add --result to get the result shown on the console. Successful execution of the command will show the following output:

{"greetings": "Hello! Welcome to OpenWhisk" }

Conclusion

With Apache OpenWhisk, you can write your functions in popular languages such as NodeJS, Swift, Java, Go, Scala, Python, PHP, and Ruby and build components using containers. It easily supports many deployment options, both locally and within cloud infrastructures such as Kubernetes and OpenShift.