Call a Workflow from a Function!

Grant Timmerman
Google Cloud - Community
3 min readJan 28, 2021

--

Workflows + Functions

Cloud Workflows allows you to orchestrate and automate Google Cloud and HTTP-based API services.

You might ask, how can you integrate these workflows with your existing Cloud Functions? — Great question.

In this post, you’ll learn how to call a workflow from a Cloud Function! 🚀

Deploy a Cloud Workflow

First you need a workflow in your project (if you don’t have one already). Create a new file called myFirstWorkflow.yaml with the following contents:

myFirstWorkflow.yaml

Reading the YAML above, the workflow does the following:

  1. Get the current date/time from a public Cloud Function.
  2. Calls the Wikipedia (opensearch) API with a query parameter dayOfTheWeek.
  3. Returns the top results from Wikipedia.

Let’s deploy the workflow with gcloud:

gcloud workflows deploy myFirstWorkflow \
--source=myFirstWorkflow.yaml

Test the workflow if you’d like with:

gcloud workflows run myFirstWorkflow

With that command, you’ll execute the workflow and wait for results.
The response will include details like the following:

result: '["Tuesday","Tuesday Weld","Tuesday Night Music Club","Tuesday (ILoveMakonnen
song)","Tuesday Group","Tuesdays with Morrie","Tuesday Knight","Tuesday (Burak Yeter
song)","Tuesday Morning Quarterback","Tuesday Vargas"]'

state: SUCCEEDED

Nice! ✔️

Create a Cloud Function

So now instead of running the workflow from gcloud, we’d like to call the workflow from a Cloud Function. You can imagine this being very useful for things like creating invoices or running an image processing pipeline.

Cloud Functions makes it fairly easy to call a workflow as we don’t need to write any auth code. Let’s create a lightweight Node function using the Functions Framework.

Create a package.json file:

package.json

This file describes our Node program, which uses the Functions Framework and Cloud Workflows API client library.

Create an index.js file:

For your function, create the following index.js file:

index.js

Here’s a brief explanation of the code:

  • exports.runWorkflow is your Cloud Function that is passed your Express (req, res) parameters. We accept POST requests, call the callWorkflowsAPI function and return the result.
  • There’s a little setup of the Cloud Workflows API client
  • The callWorkflowsAPI function creates an execution of our workflow and then awaits for the workflow to complete execution using exponential backoff.
  • We immediately return the response if we get it, otherwise we’ll timeout and return { "success": false }.

You could remove the logging and exponential backoff part to make the code shorter if you’d like.

To test on localhost, run npm i and npm start. You can test the function with a command like curl -XPOST localhost:8080.
You may need to log in with gcloud auth application-default.

Deploy to Cloud Functions

Run the following command to deploy the function to Cloud Functions:

Deploy and test our Function

We’ve made this Cloud Function private, but we can test the Cloud Function by cURLing the URL with an auth token from gcloud. The Workflow result will be in the HTTP response.

Result: ["Wednesday","Wednesday Night Wars","Wednesday 13","Wednesday Campanella","Wednesday Addams","Wednesday Night Hockey (American TV program)","Wednesdayite","Wednesday Campanella discography","Wednesday Morning, 3 A.M.","Wednesday Theatre"]

We can also check out our Stackdriver logs for the Function if there are issues.

Thanks for Reading

Now that you have a Workflow hooked up with your Cloud Functions, perhaps add a schedule to your Function with Cloud Scheduler. Your mind is the limit!

If you enjoyed this article, I’d recommend watching this YouTube video on Cloud Workflows:

Introducing Google Cloud Workflows

And check out all of our dozens of Workflow samples on GitHub:

https://github.com/GoogleCloudPlatform/workflows-samples

--

--