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:
$ eval $(minishift oc-env) && eval $(minishift docker-env)
$ oc login $(minishift ip):8443 -u admin -p admin
$ oc new-project faas
$ oc project -q
$ oc process -f https://git.io/openwhisk-template | oc create -f -
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:
$ while $(oc get pods -n faas controller-0 | grep 0/1 > /dev/null); do sleep 1; done
You can also watch the status with this:
$ while [ -z "`oc logs controller-0 -n faas 2>&1 | grep "invoker status changed"`" ]; do sleep 1; done
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:
$ git clone https://github.com/apache/incubator-openwhisk-devtools
$ cd incubator-openwhisk-devtools/java-action-archetype
$ mvn -DskipTests clean install
$ cd $PROJECT_HOM
Let’s now create a simple Java Action, deploy it to OpenWhisk, and finally, invoke it to see the result. Create the Java Action as shown below:
$ mvn archetype:generate \
-DarchetypeGroupId=org.apache.openwhisk.java \
-DarchetypeArtifactId=java-action-archetype \
-DarchetypeVersion=1.0-SNAPSHOT \
-DgroupId=com.example \
-DartifactId=hello-openwhisk \
-Dversion=1.0-SNAPSHOT \
-DinteractiveMode=false
Next, build the Java application and deploy to OpenWhisk on Minishift locally:
$ cd hello-openwhisk
$ mvn clean package
$ wsk -i action create hello-openwhisk target/hello-openwhisk.jar --main com.example.FunctionApp
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.
Comments are closed.