IBM Bluemix OpenWhisk 101: Developing a Microservice

by Alexander SologubMarch 30, 2016
Learn how to develop a simple JavaScript application, deploy it to the platform, and run Docker actions.

IBM Bluemix OpenWhisk is an event-driven compute platform that enables developers to build chains of scalable microservices. It executes application logic in response to events or through direct invocations.

Earlier, we provided an overview of OpenWhisk’s architecture and components. This post shows how to develop a simple JavaScript application and deploy it to OpenWhisk. You will also learn about Docker integration.

 

Before we start

If you want to follow my code examples, you need to set up a couple of things first:

Because OpenWhisk was only recently announced, it is not publicly available yet. It took a couple of hours for my early access to be approved.

 

A “Goodbye, sir!” app

Below I show how to write some code and actually run it. While the whole world likes to greet each other using boring “Hello, World” stuff, we are going to do the opposite. A serious application doesn’t have time for such things, so it’s going to state that it’s busy and politely excuse itself.

The source code can be found on GitHub.

I’m going to use JavaScript here, but you can also work with Swift. To start, create the goodbye-sir folder with goodbye-sir.js:

The only interesting part of this function is that it returns a JSON object with the payload key. Once we send this function into the OpenWhisk service, it will become one of the remotely executable actions. As it turns out, OpenWhisk actions are required to return valid JSON. An important thing to note is that OpenWhisk expects every action to be defined in a single file. The file can contain several functions, but at least one of them should be named main.

Now, we need to use the OpenWhisk CLI to actually deploy our code. In terms of OpenWhisk, the main function will become an action. I like to automate stuff, so let’s write our deployment code into a separate script—deploy.sh:

Note that wsk action update creates a new action called goodbye-sir if it is missing. Subsequent calls upload updated code.

And finally, let’s make the script executable and kick off the whole process:

If you see something like this, you probably haven’t set up the OpenWhisk credentials:

Another common issue is that a fresh Bluemix account doesn’t have any application spaces in the default organization. The error looks similar to this:

In this case, follow the link and create a new space. Then, you can copy the OpenWhisk CLI configuration code from here. Note that it should have the name of your new space at the end of the command.

Now, we invoke our code with the OpenWhisk CLI:

As you might have noticed, we’ve specified the --blocking parameter. You can call OpenWhisk code asynchronously by simply omitting it. The next command will query OpenWhisk for the action result.

You can also parametrize your actions. Edit goodbye-sir.js:

When you deploy and run the code, you should see something similar to:

And this basically wraps up our “Goodbye, sir!” application.

 

Docker included

Let me show you a neat trick.

As you’ve probably guessed by now, OpenWhisk can run arbitrary Docker images. Currently, support is limited to publicly available images from the Docker Hub.

ibm-bluemix-openwhisk-docker-actions

Note that the execution failed because “the action did not produce a valid JSON response.” Why? OpenWhisk uses STDIN and STDOUT to talk to the running container. All incoming parameters come into STDIN as a string representation of a JSON object. The same is true for STDOUT—OpenWhisk expects correct JSON to be printed out as the result of an operation.

Anyway, it’s great to see that Docker actions are available. Developers can work with any language or framework they like and still benefit from using OpenWhisk.

 

Conclusion

OpenWhisk is fairly easy to use. In this post, we have seen how to utilize it to run isolated blocks of code. Note that OpenWhisk does not set any limitations on runtimes, so developers can start building language-agnostic microservices right away.

On the downside, there is an issue with performance. Although OpenWhisk is still experimental, I can’t help but notice how slow Docker actions are. It takes about 10 seconds to execute a “Hello, World” container.

ibm-bluemix-openwhisk-performance

At the same time, it is not true for JavaScript and Swift actions that work much more quickly.

 

Further reading:

 


The tutorial is written by Alexander Sologub, edited and published by Victoria Fedzkovich and Alex Khizhniak.