Day #5 with Cloud Workflows: visualizing the structure of your workflow definition

So far, in our Cloud Workflows series, we have seen some of the YAML syntax for defining workflows. However, steps are defined after each other, as a series of step definitions, but in spite of the jump instructions, the conditionals, you don’t really see visually what is going to be the next potential step in a workflow execution. 

Fortunately, a new UI enhancement has landed in the Google Cloud Console: the ability to visualize a workflow definition with a graph, when you’re editing the definition. Furthermore, the graph is updated in quasi real-time as you make updates to the definition.

Let’s see this in action in the video below:

Thanks to this visualization, it’s easier to further understand how your workflow definition is structured, how executions operate. You can more easily track which steps follows a particular step.


Day #4 with Cloud Workflows: jumping with switch conditions

In the previous articles about Google Cloud Workflows, we talked about how to assign variables, create expressions, and also how to jump from a step to another. It’s time to combine both aspects to understand how we can do conditional jumps, thanks to the switch instruction.

Let’s start with a first step defining a variable, whose value we’ll use in our switch condition:

- assignement:
        - number: 42

Then we’re going to create our second step that will use a switch instruction, with an expression:

- evaluate:
        - condition: ${number > 100}
          next: highValue
        - condition: ${number < 100}
          next: lowValue
    next: end

We define two conditions with two expressions, checking if the number is above or below 100, then we go to a different step (highValue or lowValue steps). If none of the conditions are met, we go to the end of the workflow (or we could return some value or raise some error).

We also need our two steps to go to:

- highValue:
    return: "It's high!"
- lowValue:
    return: "It's rather low!"

And we’re done! If the number is 42, like in our case, the execution of the workflow will go through the lowValue step, and return the string saying that it’s a low value.

Thanks to switch conditionals, with expressions and jumps, we can have non-linear logic in our workflow definitions. In upcoming articles, we will also have a look at how to use more complex data structures like arrays and dictionaries, and how to define inputs and outputs.

Day #3 with Cloud Workflows: variable assignment and expressions

Now that we have multiple steps in our workflow definition, let’s see how we can pass data around, from a step to another.

In a step, you can assign values to variables. Those values can be ints, doubles, strings, or booleans (and also null). Use the assign keyword as follows:

- assignments:
        - two: 2
        - pi: 3.14
        - message: "Hello"
        - bool: True

Those variables are available in the whole scope of the workflow, and can be accessed in other steps. So let’s see how we can do something with those variables. Let’s add a second step to our workflow definition:

- twoPi:
    return: ${"Twice pi is " + string(two * pi)}

We are using the ${} notation to create an expression. We’re multiplying two numbers, we’re converting them to a string, and we’re concatenating two strings together, to get our final value. 

Note that not all operations are allowed on all types, so you might need to do some conversions with built-in conversion functions like the string() function in our example. There are all sorts of arithmetic operators or boolean logic operators.

For more information, you can read about variable assignments, data types, and expressions. Next time, we’ll also have a look at more complex data types.

Day #2 with Cloud Workflows: a workflow is made of steps or even jumps!

Let’s continue our discovery of Cloud Workflows!

Yesterday, we discovered the UI of Cloud Workflows. We created our first workflow. We started with a single step, returning a greeting message:

- sayHello:
    return: Hello from Cloud Workflows!

A workflow definition is made of steps. But not just one! You can create several steps. In YAML, the structure of your workflow will be something like:

- stepOne:
    # do something
- stepTwo:
    # do something else
- sayHello:
    return: Hello from Cloud Workflows!

By default, steps are executed in the order they appear, from top to bottom. The execution will finish when either you return a value, or you reach the final step. If there’s no return statement, a null value is returned as result of the workflow execution.

A small step for a workflow execution, but you can also do a jump between steps! For that, you’ll use the next instruction:

- stepOne:
    next: stepTwo
- stepThree:
    next: sayHello
- stepTwo:
    next: stepThree
- sayHello:
    return: Hello from Cloud Workflows!

Here, we jump between steps, back and forth, before going to the final step that will return a value, and thus finish the execution of our workflow.

Of course, we can go beyond a linear series of steps, and in subsequent articles, we’ll see how we can create conditional jumps and switches, for more complex logic, and how we can pass some data and values between steps.

Day #1 with Cloud Workflows: your first step to “Hello World”

With more and more interconnected services, making sense of their interactions becomes critical. With Google Cloud Workflows, developers can orchestrate and automate such complex systems by creating serverless workflows. 

In this series of articles, we will learn together how to use Cloud Workflows, and get to know all its features, with short and easy to read tutorials. For our first day, we’ll discover and use the Workflows UI in the cloud console. We will create a simple “hello world” workflow, consisting of a simple step. Going further, in the coming days, we’ll learn more about advanced features. But first things first!

In the Google Cloud console UI, you can locate Workflows in the Tools section of the hamburger menu:

You can pin this menu item, to have it stick at the top of the list.

The first time you are accessing this page, you’ll be greeted with the following screen, which will ask you to enable the Workflows API. So just click on “ENABLE”:

Once the API is enabled, you’ll be in the Workflows home screen:

Click on the “CREATE” button to create your first workflow definition:

Find a name for your workflow (it should start with a letter). You can provide an optional description. Currently, only “us-central1” is available for the beta of Cloud Workflows, but more regions will be available later on. 

Notice that we have to select a service account that Workflows will use to call other Google Cloud APIs, however here, there’s a warning telling us that the project requires a service account. As I’ve created a brand new project, I didn’t have any service account created. If you had used, for example, Cloud Functions beforehand, a default service account would have been created. If you need to create a service account, you can create one in “IAM & Admin > Service Accounts”, then use this one.

My first workflow will be called “w01-first-workflow”:

Move on to the next section with the “NEXT” button. That’s where you will define your workflow:

This first workflow consists in one single step, called “sayHello”, and whose sole purpose is to return a hello world message:

- sayHello:
    return: Hello from Cloud Workflows!

As you can see, workflow definitions are written using the YAML configuration language.

Click “DEPLOY” to deploy the workflow. You will then see the details of your new workflow. In the “executions” tab, you can see past executions.

In the “logs” section, you can see the logging messages associated with your workflow creation, deployment and executions:
And in the “definitions” section, you can see the YAML description you just created:
Now click on the “EXECUTE” button. You will see the input section (we’ll learn about input arguments in an upcoming article), and the YAML definition. Click the other “EXECUTE” button:
You will see the result of your execution (“succeeded”, with other details about the execution), as well as both the input and the output, with our greeting message:
And here you are! You created your first workflow definition, and launched the first execution of this workflow!

In the coming days, we will have a closer look at the structure of workflow definitions (its steps), how to define input arguments, but also how to create an execution of a workflow from the command-line.

© 2012 Guillaume Laforge | The views and opinions expressed here are mine and don't reflect the ones from my employer.