Compute Services

Prepping your OpenWhisk code for GA

Share this post:

OpenWhisk has moved to GA!

openwhisk-hero-imageIf you’ve already been exploring OpenWhisk and building your serverless infrastructure, you’ll want to pay attention to to several deprecations now that OpenWhisk has moved to General Availability (GA).

On January 12th, 2017 features that have been marked deprecated will no longer be supported. Some of these changes are breaking changes, meaning, that if you do not address them, your OpenWhisk actions will cease to function.

Authentication

First, and most importantly, API keys and namespaces are changing. If you head over to download the latest command line interface, you’ll see instructions to set your new authentication keys and remove the old namespace.  Note: You’ll need the latest CLI to use GA features. You can learn more about these changes here: It’s time to update your OpenWhisk API Keys.

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!"
  }
}

Other Deprecations & Changes

In addition to these coding/API changes, several packages and the Swift v2 and Node.js 0.12 runtimes have been deprecated. You can see the list of package changes here.

If you have any Node.js 0.12 actions, they will need to be migrated to the Node.js 6 runtime. If you have any Swift v2 actions, you will want to migrate those to the Swift v3 runtime.

Cloudant includeDocs Deprecation

The includeDocs parameter for the Cloudant trigger has been deprecated, and will cease to function on January 12th, 2017. This parameter was used to attach the affected document as a parameter to the document change feed (document was included in every insert, update, or delete event). This is no longer supported. The document id will still be passed in the change feed, but you will now have to query the Cloudant action/database to fetch the complete document if you need it. You can read more about this change at: Cloudant Change Trigger no Longer Supports “includeDoc”.

 


 

It’s been an exciting year as advances in serverless computing, and OpenWhisk in particular, have transformed the way that we can build our applications and APIs. We no longer have to think about provisioning servers, ramping up to scale to meet demand, or building monolithic server architectures. Serverless computing enables us to think in terms of discrete, composable (and reusable) microservices that automatically scale based on demand, or automatically process input in near real time based on activity and events.

With all of this information, you should now be all set to move your OpenWhisk actions to GA. We can’t wait to see what you’ll build next!

More Compute Services stories
December 11, 2018

Unifying Containers, Apps, and Functions

Innovative solutions like Knative and Istio are leading us to a unified container application platform that lets developers leverage the best of containers, apps, and functions in a single integrated way.

Continue reading

December 11, 2018

Using Availability Zones to Enhance Event Streams Resilience

With the Enterprise plan of IBM Event Streams, you can deploy Kafka across availability zones to maximize both its resilience to failures and the durability of your message data. Applications can use Kafka to achieve the right balance of availability and durability to meet your business needs.

Continue reading

December 10, 2018

The Run Up to KubeCon: Easing the Burden of Security and Infrastructure Management

In the run up to KubeCon, IBM Cloud announced new capabilities to ease Kubernetes operations and improve security across multiple cloud architectures.

Continue reading