Running Go Programs as IBM Cloud Functions
IBM Cloud Functions is a functions-as-a-service platform that is powered by Apache OpenWhisk. It is a readily extensible serverless platform that supports functions authored in various programing languages including Node.js,Python, Swift, Java, and PHP.
A feature of IBM Cloud Functions that has been around for some time but not well documented is support for native binaries: any executable that is binary compatible with a standard OpenWhisk container may run as a serverless function.
This approach affords you an even greater choice of languages for your serverless applications. You can develop using Go, Rust, and even C and C++. Moreover, functions may be composed irrespective of their implementation language, empowering you to use the right language for the task at hand while building complex serverless applications.
The native executable must conform to the following conventions:
The program shall accept a single command line argument as input. The argument is a JSON object that is encoded as a string. The object represents the input argument to the function.
The program must return a JSON object as a JSON formatted string, emitted to
stdoutas the final log line before the program terminates.
The program may log to
stderrotherwise as desired.
The executable program must be called
execand should be self-contained (e.g., statically linked). Otherwise, make sure to package all dependencies.
Showing not telling
Save the code above to a file
sample.go and build it. You must set the
GOARCH properties to cross-compile the binary if you build the binary on another platform. The executable must be called
The rest is standard OpenWhisk workflow to create an action. The executable must be Zip compressed. And the action is created using the
--native parameter to indicate this is a native executable using the
wskcommand line interface.
The action is invoked as any other action.
Logs may be fetched using
wsk as well.
The Go action may be turned into a web action, to make it accessible via a browser or
You run a Rust action in a similar way using the steps described in another blog. In general, any binary that you statically compile may run as a native action. This makes IBM Cloud Functions, or your own deployment of OpenWhisk, a flexible platform for serverless computing. Try it and join the growing OpenWhisk community on Slack. You can also contribute on GitHub.