Serverless functions in your favorite language with OpenWhisk

Apache OpenWhisk is a readily extensible serverless computing platform that supports functions authored in many programing languages including Node.js, Python, Swift, Java, and PHP. The set of supported languages extends beyond these five however, and includes Go as well as other compiled languages. In fact, a feature of OpenWhisk that has been around for some time, but not well documented, is its support for native binaries. With a native binary, any executable that is compatible with a standard OpenWhisk container may run as a serverless function, also known as an action.

The native binary approach affords you more languages to choose from for your serverless applications, such as Go, Rust, C and C++ to name some. Not to mention, you can also bring your own Docker container and run it as a function.

I recently added a quick guide for running Go programs as OpenWhisk actions. Thanks to James Thomas, there’s also a tutorial for Rust actions. You can even run COBOL programs as actions (check out this YouTube video for a demo).


In OpenWhisk, actions run within managed Docker containers. The platform’s architecture permits bespoke Docker images for each language runtime. This approach has permitted IBM Cloud Functions, for example, to offer Node v8 support with SDKs that are specific to the IBM Cloud portfolio, including Watson services and Cloud Object Storage. Another recent addition will bring a Python image with packages popular for numerical analysis and machine learning.

You can find out the supported runtimes and images specific to an OpenWhisk deployment from the manifest accessible at the root of the API host. For example: https://openwhisk.ng.bluemix.net for IBM Cloud Functions, or your localhost for a private deployment.

The OpenWhisk community has demonstrated some of the extensibility of the platform, notably with the addition of PHP support by Rob Allen. We welcome GitHub contributions (C# is on the wishlist) and encourage interested developers to join the conversation with the growing community on Slack.

A recent discussion on GitHub around runtimes raised the issue of running Bash scripts as serverless functions. With native action support, that’s also readily supported in OpenWhisk. Here’s a brief example using Bash to flip a coin N times and return the number of times the coin is heads or tails.

#!/bin/bash
# install jq if it does not exist
if [ ! -f /usr/bin/jq ]; then
apk update && apk add jq
fi

# determine number of flips
N=`echo "$@" | jq '."n"'`

# total count of heads and tails
HEADS=0
TAILS=0

for i in `seq 1 $N`; do
echo -n "flipping coin..."
if [ $(( RANDOM % 2 )) == 0 ]; then
echo "HEADS"; HEADS=$(( HEADS + 1 ))
else
echo "TAILS"; TAILS=$(( TAILS + 1 ))
fi
done

echo "{\"trials\": $N, \"heads\": $HEADS, \"tails\": $TAILS}"

Saving the code above to a file flip.sh, the wsk CLI turns the rest into a standard action creation and invocation.

wsk action create flip --native flip.sh
wsk action invoke flip --result --param n 100
{
"heads": 44,
"tails": 56,
"trials": 100
}

Parting Thought: The polyglot nature of actions is noteworthy in the context of function composition. That is, actions may be composed irrespective of their implementation language. As a result, developers are empowered to use the right language for the task at hand while building complex serverless applications. The platform’s support for a wide range of languages, with a canonical interface between functions, protects existing investments in legacy code, provides a gentle migration path to the cloud, and doesn’t force developers to give up their language of choice for the sake of adopting the serverless computing paradigm.