An Example of an OpenWhisk Sequence

Raymond Camden
Jan 11, 2017 · 4 min read

This isn’t going to be terribly complicated, but as I thought it was kinda cool, I thought I’d share it. I’m currently working through the OpenWhisk workshop, a Node School-ish set of problems/tasks to help you learn the basics of OpenWhisk. One of the problems has you build a sequence, and while I recommend folks try to solve the problem for themselves, I thought I’d share my solution and what I discovered while working on it.

Sequences are what you would imagine, two or more actions that are linked together. You define them by listing the actions you want executed, and when run, OpenWhisk will execute each in order and pass the output from one to the input of the other. (And this brings up an important detail I’ll cover in a moment.)

Before I talk about the problem from the workshop, let’s build a simple example. First we’ll build an action that takes a string input and returns the size of the string. To be clear, this is trivial to the point of stupidity, but let’s keep it nice and simple.

function main(params) {    return {

I called this action strlen. Now let’s create an action that, given a numeric input, finds the prime numbers. I’m going to use the solution from this Stack Overflow answer:

function getPrimes(max) {
var sieve = [], i, j, primes = [];
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
// i has not been marked -- it is prime
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
return primes;
function main(params) {
return {

It takes a parameter of number and returns an array in a key called primes.

As a quick aside, I used the OpenWhisk extension for Visual Studio Code to create and test these actions. While the extension is still in development, it works darn well so far.

Ok, so how do we make a sequence?

First off, you create a sequence very much like you do an action, in fact, the command begins the same way:

wsk action create name

But this is where things differ — instead of passing a filename of a local file for your action, you pass the –sequence argument and a list of actions:

wsk action create name --sequence a,b,c

Ok, so for our code above, I’d do this:

wsk action create strToPrimes --sequence strlen,primes

Once done, you invoke it just like a normal action:

wsk action invoke strToPrimes -b -r -p text "Raymond"

Notice I’m passing in my argument text that is expected by my first action. And it works perfectly! (Not!)

Image for post
Image for post

What went wrong? Did you notice the output of strlen was an object with the key “length”? Did you notice the input for primes was expecting a param called “number”? When building sequences, you have to ensure that your output/inputs match up. In general, this shouldn’t be a big deal. I’m ok with changing strlen to output “number” instead, but I do wish that OpenWhisk would let me define sequences along with a way to ‘map’ the output/input values from action to action.

Now it works just fine:

Image for post
Image for post

Ok, so lets look at the workshop problem:

  • Create an Action which takes a string parameter (text) containing a sentence. Return an array containing the individual words within the sentence.

“Hello my name is James” –> [“Hello”, “my”, “name”, “is”, “James”]

  • Create an Action which takes an array of words and returns an array with those words reversed.

[“Hello”, “my”, “name”, “is”, “James”] –> [“James”, “is”, “name”, “my”, “Hello”]

  • Create an Action which takes an array of words and returns the string createdby joining the words together into a sentence.

[“James”, “is”, “name”, “my”, “Hello”] –> “James is name my Hello”

  • Create a new Action (reverse-sentence-words) using a sequence that joins together these three Actions.
  • This new Action should take a parameter (text) and return a sentence (text) which contains the words in the string reversed.

All in all, fairly trivial. My first action is just this:

function main(params) {    var words = params.text.split(' ');
return {array:words};

My second one is just this:

function main(params) {    return { array:params.array.reverse()};


And my final one is:

function main(params) {    var result = params.array.join(' ');
return {text:result};

Here is my sequence in action. By the way, that -r argument means just return the result - I like that.

Image for post
Image for post

Originally published at:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store