“Hello, World” in IBM Cloud Functions vs Express Serverless Platform — A Developer’s Perspective

Shashikanh
IBM Cloud
Published in
5 min readJul 25, 2019

IBM Cloud Functions are IBM Cloud’s equivalent to AWS Lambda. Like Lambda, with IBM Cloud Functions, you can write custom functions and expose them via an HTTP interface through either IBM Cloud Function’s minimal HTTP configuration or IBM API Connect for more sophisticated use cases. Express Serverless Platform, an open source solution, offers a similar stack that you can use to write Node.js APIs and expose those APIs through Express Gateway without vendor lock-in.

In this article, I’ll walk through setting up a rudimentary Node.js function in both Express Serverless Platform and IBM Cloud, and discuss the differences between the two.

IBM Cloud

Go to IBM’s OpenWhisk landing page and log in. IBM Cloud is the new name for what used to be called Bluemix.

OpenWhisk is an Apache open source project that provides the backbone for IBM Cloud Functions.

After you log in, click the “Actions” tab and then click “Create.”

For this simple example, click “Quickstart Templates” to get a list of ready-made “actions.” In OpenWhisk, an action is just another name for a function.

Select the “Hello World” function template.

In the next step, indicate a package name, a runtime, and the actual code. A package is just a container for one or more cloud functions, potentially with shared configuration. IBM Cloud completes the form with the package name “hello-world”, Node.js 8 as the runtime, and a simple “Hello World” function as the code. You shouldn’t need to make any changes, just press the “Deploy” button.

Now you have your first cloud function. You can run the function, but it is not accessible via HTTP. Click the “Endpoints” tab to create an endpoint for this function.

Now, click the “Enable as Web Action” check box to expose this action via REST API. A web action has a couple of special properties that are different from regular actions. First, a web action must return an object that is serializable into JSON, or a promise that resolves to such an object. Second, if a web action returns an object that has any of the following properties, OpenWhisk uses those properties to structure the HTTP response, rather than just stringifying the resulting JSON object:

headers

statuscode

or

body

After you make your action into a web action, you should be able to access it via REST API. Click the copy icon in the “HTTP Method” panel to copy the URL for your function. You should then be able to use curl to access your function via HTTP. Keep in mind that IBM Cloud Functions do not have any authentication by default, so you don’t need an API key.

Running curl on the URL then runs your function and gives you the result.

$ curl https://openwhisk.ng.bluemix.net/api/v1/web/val%40karpov.io_dev/hello-world/helloworld.json
{
“greeting”: “Hello stranger!”
}
$

Express Serverless Platform and Express Gateway

In Express Serverless Platform, first, create a new function and name it ‘myfunction’:

Node.js functions in Express Serverless Platform have the same function signature as native Node.js HTTP request listeners. The function to print ‘Hello, world!’ with Express Serverless Platform is shown in the following screen capture. Note that the exported function must have the same name as the function name you assigned in the Express Serverless Platform GUI.

Note that this function does not have to create an actual HTTP server. Express Serverless Platform handles creating a server for you, so you can write “serverless” functions without having to write an express application. You do still get access to the Node.js HTTP response, so you can set HTTP headers and set the HTTP status code.

From the menu, add a gateway. This action adds an Express Gateway instance to your project.

Click the plus icon next to “Policies” to add a policy to the default Express Gateway pipeline. In Express Gateway, a pipeline is a sequence of policies for handling a request. Add an empty ‘proxy’ pipeline.

Drag a line from your gateway to your function, and Express Serverless Platform automatically connects your proxy policy to your function.

Express Serverless Platform also creates a function endpoint for you. Keep the “paths” for your function endpoint empty for now. Click the “settings” icon to find the publicly accessible URL for your Express Serverless Platform project.

Running curl on the URL now runs your custom function and gives you the “Hello, world!” text.

Moving On

You can use a combination of Express Serverless Platform and Express Gateway to avoid vendor lock-in.

IBM Cloud Functions are built on OpenWhisk. The possibility to migrate your IBM Cloud Functions deployment onto OpenWhisk is there. Head over to www.express-serverless.io for full details, documentation, and installation instructions!

Express Serverless Platform scaffolds out an app for you and stores it in a Git repo that you can clone. That enables you to run your app locally or on any cloud provider. Express Serverless Platform does automatically host your app for you, but that isn’t your only option.

Latest Update: Express Serverless Platform is now an open source project!

IBM Cloud is appealing because IBM is a well-established company. Using IBM Cloud Functions means you must run your code on IBM Cloud and manage your API using IBM API Connect.

I’ve done up a detailed comparison analysis on features, pricing, and more between Express Serverless Platform running on IBM compared to IBM Cloud Functions, which you can download for free here.

Additionally, if you’re interested in more of these topics, join the live discussion on Twitter (@lunchbadger) or (@express_gateway)

And thank you for sharing!

Author: Valeri Karpov for LunchBadger, Inc.

--

--