In a previous post we saw an Apache OpenWhisk hello world, to install OpenWhisk on Kubernetes and then create and launch a first action, that is a serverless function.
In this post we see how to create a sequence of actions in OpenWhisk, i.e. connect multiple serverless functions together in series.
A sequence can consist of two or more actions, or serverless functions, where the output of each action is the input of the next.
The resulting sequence can then be considered as an action, whose input will be the input of the first action of the sequence and the output of the last action of the sequence.
To make an example of a sequence, let’s go back to the OpenWhisk hello world seen previously and add a second action that will take as input the output of the action that was already present.
Below is the JSON output of the first action “helloWorldAction.js”, already present in the hello world. In the “message” property we also find the name of the pod that performed the action, which also indicates the type of runtime used.
This will be the input of the action we are going to add, with which we will extract the runtime used from the pod name and then create the next output.
Here the code of the second action “getRuntimeAction.js”.
function main(params) {
const hostname = JSON.parse(params.body).hostname;
const runtime = hostname.split('-')[5];
const message = `The runtime used by the first action is: ${runtime}`
const body = JSON.stringify({
message
})
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body
}
return response
}
Now let’s create this second action.
wsk -i action create getRuntimeAction getRuntimeAction.js
Then we can create the sequence using the two actions.
wsk -i action create mySequence --sequence helloWorldAction,getRuntimeAction
Invoke the sequence, using the input of the first action.
wsk -i action invoke --result mySequence --param name Roberto
Check the output of the sequence.
Next time we will see how to use OpenWhisk’s triggers and rules to activate multiple actions in parallel instead.