While exploring IBM Bluemix, I came across a service called OpenWhisk. OpenWhisk is service on Bluemix cloud platform which provides what is known as “Serverless” computing for your applications. Serverless as a word and technology is becoming interesting & one of the hottest trends in the cloud world. OpenWhisk is pretty much like the offering from Amazon known as Lambda & Microsoft known as Functions. In this blog post let’s take a lap around OpenWhisk on Bluemix.
What is Serverless?
When I looked up for a definition of “Serverless” on Wikipedia, below is what I could find:
Serverless computing is a cloud computing execution model in which the cloud provider dynamically manages the allocation of machine resources. Pricing is based on the actual amount of resources consumed by an application, rather than on pre-purchased units of capacity.
Don’t be confused by the term Serverless. It doesn’t mean that you don’t have a server but it means that the server management and server capacity planning decisions are not done by you – the developer – rather somebody does it for you. So, you focus only on the code you want to write and when to execute it. All other things are taken care for you.
Serverless Computing on Bluemix:
The emerging trend of cloud native, event driven workloads or applications made way for a serverless computing platform on Bluemix called “OpenWhisk”. OpenWhisk is an open source cloud platform that executes code in response to some events. Some features of OpenWhisk:
- Multiple Programming Languages – JS, Python, Java, Swift
- Asynchronous Computing – uses pub/sub message queues
- Action chaining – create sequence of actions, actions can be in any language
- Integrated Container Support – actions run in docker containers
Fundamentals of OpenWhisk:
Before we deep dive into getting started with OpenWhisk – we need to first understand some key terminologies that OpenWhisk operates on. The following 3 things are important to understand:
Triggers are events emitted by data sources. Example includes – change to database records, new code commit to your repo, HTTP request from web/mobile app. The events from the sources are channeled through a trigger.
Rules associate a trigger with an action. Every time a trigger fires, the rule invokes the associated action.
Sequences are chain of actions. You can chain actions and they will be invoked in order. Output of one action becomes input of the next.
Getting started with OpenWhisk:
The pre-requisite to getting started with OpenWhisk is to get Bluemix credentials. If you don’t have one, you can sign up for a Bluemix trial here. For the rest of the post I will assume you have access to your Bluemix console.
Log into your Bluemix console and click on Catalog link available on top. Then select OpenWhisk from Apps section. Click “Start Creating” button on “Getting Started with IBM OpenWhisk”. You will then be presented with OpenWhisk workspace.
When OpenWhisk workspace is launched, you will be taken to Manage Actions section. Here you will see a list of Actions that you have created. By default, 3 actions will be listed in the grid. The default packages are simple Hello World examples & a sequence example. Let’s create an action of our own. Click on “Create Action” button. Provide a name for your action, select a runtime and click Create. I have selected NodeJS as the environment.
Once the action is created, we will be taken to a code editor. Here is where you can code your action or function and save. You can invoke the action right from the editor to see if everything works fine. Here is my code editor screenshot:
I have created a simple action and named it as “PingAction” – a function which when pinged will respond with a message “Ping Ok”. Save the code changes by clicking the “Save” button. We also have the option to invoke our function right within the editor:
At this point, you can go to “Additional Details” section and find out the URL for your action. The endpoint URL of the action which we created just now will be specified under “REST API” section. If we use this to ping the action – we will need to pass the authentication headers. Later we will create a API gateway and put our action behind the gateway.
Creating Triggers & Rules:
As mentioned earlier, triggers are nothing but a channel for events that may occur. Events from internal or external sources will be channeled through a trigger. Rules allow our actions to react to the events.
In the code editor of the action we created, click “Triggers”. We should see option to create a new Trigger. Click on button “Create Trigger”. Next up we will need to choose a Trigger type. By default many Bluemix offering provide their own events which can be connected – for e.g. Cloudant database, Message Hub, Mobile Push. We also have provision to create a custom trigger. There is also a handy Periodic trigger – invoke the action at periodic interval.
For the sake of this blog post, I will be selecting Periodic trigger. Click on Periodic trigger option. In the Connect Tigger screen, provide a name for the trigger, select the periodic interval i.e. day & time. I have selected PingAction to be invoked every hour every day. Click “Create & Connect” button.
Using Develop Interface:
If you navigate back to OpenWhisk workspace, you will see a Develop menu option on the sidebar. Click on Develop menu and you will be presented with an interface where you can see all the actions you have created, the triggers, the rules and sequence (which we will see next) in one place. Here is a screen shot of how my interface looks like:
Notice the rule & trigger we created earlier are also listed here. We can fire the trigger right from this interface and test that our action is invoked.
One of the powerful features of OpenWhisk is sequencing. You can link actions to create a sequence. Output of one action becomes input of another action in the sequence. Let’s create a sequence for our PingAction. We will make use of a system provided utility action called Echo i.e. echoes whatever comes as input to the action. From your OpenWhisk workspace, select Develop option. Select your action from the “My Actions” section. Next click “Link into a Sequence” button at the bottom of the screen.
Next, in the “Configure a New Action Sequence” scroll down till you see “UTILS” package and select it.
Next, select “echo” action which is available in the package. Leave the defaults as is and click the button “Add to sequence”
Confirm the new sequence by clicking “This Looks Good” button, provide a name and save the sequence. This was a simple example of how to sequence actions. The possibilities are left to your imagination. This from my perspective is one of the strengths of OpenWhisk.
From your OpenWhisk workspace, you will see an option in the side bar named “API”. This option lets you manage your Actions through an API gateway. This API gateway (when created) acts as a proxy to actions and provides additional features like HTTP method routing, client id/secrets, rate limiting and CORS. From the API interface click “Create Managed API” to create a API gateway. In the “Create API for OpenWhisk”, provide API name and base path for your API. Use Create Operation” tp map OpenWhisk actions to HTTP operation.
Set the security and rate limiting according to your needs and click “Save & expose”. This is another great feature of OpenWhisk & Bluemix combined. Availability of API gateway intrinsically within OpenWhisk is a benefit and allows us the creators to create n number of actions and map them to appropriate HTTP operation like GET, PUT, POST, DELETE etc. The API gateway interface provides a handy API explorer which you can use to test your API endpoints.
With API gateway, you never expose your actions directly to outside world. The API gateway endpoints is what you can expose to your applications. You can add appropriate authentication on the API gateway itself and secure your endpoints.
With cloud native applications becoming more and more prominent, the development paradigm is moving more towards platforms & runtime versus the infrastructure. We, as application developers can focus completely on the logic we need to execute compared to server configuration and other infrastructure concerns. Serverless means freeing ourselves from infrastructure concerns and more focus on business values of our applications.
OpenWhisk as a serverless platform or service offering on IBM Bluemix is an interesting offering. In this blog post which took a quick lap around what OpenWhisk is, what are the core concepts of OpenWhisk and how to get started quickly. What I showcased in this post was a basic hello world kind of application. I believe the potential of OpenWhisk is left to your imagination. The simplicity of creating actions in language of your choice, sequencing of actions and API gateway are a bug plus of the platform. Do give OpenWhisk a try. Do let us know your OpenWhisk experience by writing a comment below.