Skip to content

Instantly share code, notes, and snippets.

@vkarpov15 vkarpov15/ibm.md Secret
Last active Mar 28, 2018

Embed
What would you like to do?
"Hello, World" in LunchBadger vs IBM Cloud Functions

IBM Cloud Functions are IBM Cloud's equivalent to AWS Lambda. Like Lambda, IBM Cloud Functions let you 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. LunchBadger offers a similar stack that lets you 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 LunchBadger and IBM Cloud, and discuss the tradeoffs of using one over the other.

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, so don't be surprised by the bluemix.net URL. OpenWhisk is an Apache open source project that provides the backbone for IBM Cloud Functions.

Once you've logged in, click on the "Actions" tab on the left and then click "Create."

For this simple example, click on "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.

The next step asks you for 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 will fill in the form with package name "hello-world", Node.js 8 as the runtime, and a simple "Hello World" function as the code, so you shouldn't need to make any changes, just hit the "Deploy" button.

Great, now you have your first cloud function. You can execute the function, but it is not accessible via HTTP. Click the "Endpoints" tab on the left to create an endpoint for this function.

Now, click the "Enable as Web Action" checkbox to expose this action via REST API. A web action has a couple special properties as opposed to a regular action. 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 a headers, statusCode, or body property, OpenWhisk will use those properties to structure the HTTP response, rather than just stringifying the resulting JSON object.

Once you've made 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 IBM Cloud Functions do not have any authentication by default, so you don't need any API key.

Running curl on the URL will then execute your function and give you the result.

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

LunchBadger and Express Gateway

In LunchBadger, first create a new function and name it 'myfunction':

Node.js functions in LunchBadger have the same function signature as native Node.js HTTP request listeners. The function to print 'Hello, world!' with LunchBadger is shown below. Note that the exported function must have the same name as the function name you assigned in the LunchBadger GUI.

module.exports = {
  myfunction (req, res) {
    res.end('Hello, world!');
  }
};

Note that this function does not have to create an actual HTTP server. LunchBadger handles creating a server for you, so you can write "serverless" functions without having to actually 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 on the left, add a new gateway. This will add 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 LunchBadger will automatically connect your proxy policy to your function.

LunchBadger will also create a function endpoint for you. Keep the "paths" for your function endpoint empty for now. Click on the "settings" icon to find the publicly accessible URL for your LunchBadger project.

Running curl on the URL will now run your custom function and give you the "Hello, world!" text.

$ curl http://gateway-148-dev.lunchbadger.io
Hello, World!
$   

Moving On

The combination of LunchBadger and Express Gateway is a great alternative to IBM Cloud Functions if you're looking to avoid vendor lock-in. IBM Cloud Functions are built on OpenWhisk, so in theory you should be able to migrate your IBM Cloud Functions deployment onto OpenWhisk, but IBM doesn't provide any documentation on how to do that. On the other hand, LunchBadger 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. LunchBadger does automatically host your app for you, but that isn't your only option. IBM Cloud is appealing because IBM is a well-established company, but using IBM Cloud Functions means you must run your code on IBM Cloud and manage your API using IBM API Connect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.