DEV Community

Upkar Lidder
Upkar Lidder

Posted on • Originally published at Medium on

It’s now even easier to get started with Serverless using the new standalone Apache OpenWhisk!

Subset of OpenWhisk runtimes

One of the cool things about OpenWhisk was the ability to start a local copy and get coding very quickly. James Thomas has an awesome post on this — Starting OpenWhisk in Sixty Seconds. This also enabled developers to create and test their Serverless solutions on their machines. The amazing OpenWhisk community have taken this one step further. Chetan Mehrotra recently added code to enable running OpenWhisk as a standalone jar! How cool is that!

At a high level, they have taken out CouchDB and Kafka and replaced them with in memory persistence layer and a queueing system. The controller and invoker have also been slimmed down. There is obviously more to it and is well captured in the following links.

So how do you get started?

Step 1: Build the jar file

  1. You can build it yourself by following the steps in the official repo. The final jar will be available in the /bin folder. This is the preferable approach as you always get the latest features
  2. Alternatively, if you must insist, you can download the pre-built jar file from here. I am not sure if this will be kept up to date. So try at your own risk!

Step 2: run the jar file

Once you have the jar file, you can run it as follows

java -jar bin/openwhisk-standalone.jar

There are numerous other options available, but this will suffice for now. That’s it! You have a Serverless platform running on your local machine. You can run all your favorite wsk commands just as your would with a production level distributed OpenWhisk installation!

Step 3: create and deploy a simple action

If you have never deployed an action on Apache OpenWhisk or IBM Cloud, you can follow these steps

3.1 Download the wsk cli.

3.2 Create your function

function main(args) {
 if (args && args.name) {
 console.log(`hello ${args.name}`);
 return { msg: `hello ${args.name}` };
 } else {
 console.log(`hello world`);
 return { msg: `hello world` };
 }
}
Enter fullscreen mode Exit fullscreen mode

3.3 Before you deploy your function/action, you need to set the auth property using the wsk cli. This command was provided to you when you started the jar file. Simply copy paste in terminal!

  1. That’s it! Let’s deploy the index.js file as an action
$ wsk action create hello index.js
ok: created action hello
Enter fullscreen mode Exit fullscreen mode

We can now invoke this action

$ wsk action invoke hello -r
{
 "msg": "hello world"
}

# with params
$ wsk action invoke hello -r -p name upkar
{
 "msg": "hello upkar"
}
Enter fullscreen mode Exit fullscreen mode

If you were paying close attention you would have noticed that when you run the jar file, it opens a browser with the new OpenWhisk Function Playground! This makes it even easier to write and test your functions if you are new to OpenWhisk. Go ahead and try it out!

OpenWhisk Function Playground

How cool was that! If you like this, please give the repo some love! More to come on how this blazingly fast jar can help developers in their Serverless journeys.

Top comments (1)

Collapse
 
liusy58 profile image
liusy58

I followed your steps above but I got the error below:

wsk activation get d01cc7cec9f14ad89cc7cec9f1fad8f3
ok: got activation d01cc7cec9f14ad89cc7cec9f1fad8f3
{
    "namespace": "guest",
    "name": "hello",
    "version": "0.0.1",
    "subject": "guest",
    "activationId": "d01cc7cec9f14ad89cc7cec9f1fad8f3",
    "start": 1629358842856,
    "end": 1629358842856,
    "duration": 0,
    "statusCode": 3,
    "response": {
        "status": "whisk internal error",
        "statusCode": 0,
        "success": false,
        "result": {
            "error": "Failed to provision resources to run the action."
        }
    },
    "logs": [],
    "annotations": [
        {
            "key": "path",
            "value": "guest/hello"
        },
        {
            "key": "waitTime",
            "value": 208986
        },
        {
            "key": "kind",
            "value": "nodejs:10"
        },
        {
            "key": "timeout",
            "value": false
        },
        {
            "key": "limits",
            "value": {
                "concurrency": 1,
                "logs": 10,
                "memory": 256,
                "timeout": 60000
            }
        }
    ],
    "publish": false
}

Enter fullscreen mode Exit fullscreen mode