Unlock the Power of Serverless Architecture with Apache OpenWhisk

Imagine building and deploying applications without the hassle of managing servers. Apache OpenWhisk, a mature and feature-rich serverless framework, makes this a reality. With its open-source platform, developers can enjoy high scalability, fast time-to-release, and lower costs.

What is Apache OpenWhisk?

Apache OpenWhisk, developed by the Apache Software Foundation, supports a wide range of programming languages, including Node.js, Swift, Java, Go, and Scala. Compared to other mainstream serverless platforms, OpenWhisk offers similar capabilities, such as a rich programming model, broad language support, scalability, and efficiency. However, its open-source nature provides the freedom to deploy on-premises or to a cloud provider, avoiding vendor lock-in.

Setting Up OpenWhisk Locally

To set up a local OpenWhisk development environment, you’ll need Docker with Kubernetes enabled and Helm as the package manager for the local Kubernetes cluster. Follow these steps:

    1. Clone the Apache OpenWhisk Kubernetes Deployment repository:
git clone https://github.com/apache/openwhisk-deploy-kube.git
    1. Deploy Charts:
helm install openwhisk openwhisk-deploy-kube/charts/openwhisk
    1. Install the OpenWhisk CLI:
wsk property set --apihost 

localhost:31001

 --auth 

23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP7

Building a Serverless Node.js App

Using the Serverless framework, an open-source npm package, we can deploy serverless functions into various platforms. Create a Node.js app skeleton using the predefined template from Serverless:

npx serverless create --template aws-nodejs --path my-openwhisk-app

Explore the project structure, including the serverless.yml file and handler.js file.

Creating Actions and Triggers

Learn how to create a userCreate action to add a new user:

function userCreate(args) {
  // Add new user logic here
  return { message: 'User created successfully!' };
}

Update the serverless.yml file to add the configuration of the new function:

functions:
  userCreate:
    handler: handler.userCreate
    events:
      - trigger: newuserTrigger

Deploy the service:

wsk action create userCreate handler.js --kind nodejs:14

Explore how to call an external API from an OpenWhisk action, sending a Slack message to a Slack app channel:

const slackUrl = 'https://slack.com/api/chat.postMessage';
const slackChannel = '#general';

function sendSlackMessage(args) {
  const headers = {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${args.slackToken}`,
  };

  const payload = {
    channel: slackChannel,
    text: 'Hello from OpenWhisk!',
  };

  return fetch(slackUrl, { method: 'POST', headers, body: JSON.stringify(payload) });
}

Configuring Triggers and Rules

Understand how OpenWhisk triggers and rules work together to associate triggers with actions. Create a trigger and rule to post a Slack message after a new user is created:

wsk trigger create newuserTrigger --feed /_/slack/sendSlackMessage
wsk rule create postSlackMessage --trigger newuserTrigger --action userCreate

Modify the createUser function to invoke the newuserTrigger programmatically:

function createUser(args) {
  // Add new user logic here
  wsk.trigger.invoke('newuserTrigger', args);
  return { message: 'User created successfully!' };
}

Creating Action Sequences

Discover how to perform multiple tasks one by one using OpenWhisk’s sequences feature. Create a sequence based on the userCreate and listUser actions:

wsk action create listUser handler.js --kind nodejs:14
wsk sequence create createUserToListUser --actions userCreate,listUser

Link them together to make a sequence:

wsk sequence update createUserToListUser --actions userCreate,listUser

Deploy the service again and test the newly-created action and sequence:

wsk action invoke createUserToListUser --blocking

Leave a Reply