Updates to OpenWhisk Node.js actions

Andrew Trice
Apache OpenWhisk
Published in
2 min readDec 16, 2016

If you’ve already been exploring OpenWhisk and building your serverless infrastructure, you’ll want to pay attention to some recent changes when building your OpenWhisk actions.

Several Node.js API features have been deprecated — many of these changes are breaking changes, meaning, that if you do not address them, your OpenWhisk actions will cease to function.

Node.js Runtime API Changes

Inside of Node.js actions, the global whisk object, and all of its methods have been deprecated. This includes:

whisk.invoke, whisk.trigger, whisk.error, whisk.done, whisk.async, whisk.setAuthKey, whisk.getAuthKey

What does that mean for you?

First, you need to require the OpenWhisk npm package and instantiate the openwhisk instance before you can use it:

var openwhisk = require('openwhisk’);function main(params) {
var wsk = openwhisk()
//do stuff
}

Invoking Actions and Triggers

If you were invoking actions using whisk.invoke, you need to now use the OpenWhisk npm module’s action.invoke() method. For example:

function main(params) {
var wsk = openwhisk()
return wsk.actions.invoke({
actionName: “myAction",
params: { myParam: "the value" }
});
}

If you were firing triggers using whisk.trigger, you need to now use the OpenWhisk npm module’s trigger.invoke() method.

function main(params) {
var wsk = openwhisk()
return wsk.triggers.invoke({
triggerName: “myTrigger”
});
}

Environment Variables

The whisk.getAuthKey and whisk.setAuthKey methods no longer exist. You can now access the API host, namespace and API key values as environment variables.

Example (from helloContext.js):

function main(args) {
return {
"api_host": process.env['__OW_API_HOST'],
"api_key": process.env['__OW_API_KEY'],
"namespace": process.env['__OW_NAMESPACE'],
"action_name": process.env['__OW_ACTION_NAME'],
"activation_id": process.env['__OW_ACTIVATION_ID'],
"deadline": process.env['__OW_DEADLINE']
}
}

Asynchronous Action Changes

The behavior and implementation of asynchronous JavaScript actions has also changed. This change affects any actions which may have used whisk.async in conjunction with whisk.done and whisk.error.

Previously, you may have written an asynchronous action by returning whisk.async from main, and then use whisk.done or whisk.error to signal completion (or error) of the asynchronous operation. Something like this:

function main() {
setTimeout(function() {
if (error) {
return whisk.error();
} else {
return whisk.done({done: true});
}
}, 2000);
return whisk.async();
}

In the latest Node.js v6 actions, you should now use promises. Inside of the promise you will use either the resolve or reject method to complete the asynchronous action of the promise.

Something more like this:

function main(args) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (error) {
reject(error);
} else {
resolve({ done: true });
}
}, 2000);
})
}

You can check out the OpenWhisk docs to learn more about creating asynchronous actions, or check out an asynchronous sample here.

Synchronous actions remain the same… You can return a value from main, instead of returning a Promise. For example:

function main(args) {
return {
payload:”Hello World!"
}
}

--

--

Andrew Trice
Apache OpenWhisk

Technical Product Manager at IBM — pushing the boundaries of Mobile + Cloud + Cognitive + IoT + Serverless.