Deploying multi-YAML Workflows definitions with Terraform

Enhance DDoS protection & get predictable pricing with new Cloud Armor service
May 12, 2021
Azure webinar series: Reduce Costs and Drive Business Value with SAP on Azure
May 12, 2021
Enhance DDoS protection & get predictable pricing with new Cloud Armor service
May 12, 2021
Azure webinar series: Reduce Costs and Drive Business Value with SAP on Azure
May 12, 2021

I’m a big fan of using Workflows to orchestrate and automate services running on Google Cloud and beyond. In Workflows, you can define a workflow in a YAML or JSON file and deploy it using gcloud or using Google Cloud Console. These approaches work but a more declarative and arguably better approach is to use Terraform

Let’s see how to use Terraform to define and deploy workflows and explore options for keeping Terraform configuration files more manageable. 

Single Terraform file

Terraform has a google_workflows_workflow resource to define and deploy workflows. For step-by-step instructions, see our basic Terraform sample, which shows how to define a workflow in main.tf and how to deploy it using Terraform.

Let’s take a closer look at how the Workflows resource is defined in Terraform:

You can see that everything about the workflow, such as name, region, service account, and even the workflow definition itself are defined in this single file. While this is workable for simple workflow definitions, it’s hardly maintainable for larger workflows. 

Importing a Workflow definition file

A better approach is to keep the workflow definition in a separate YAML file and import that into Terraform. The templatefile function of Terraform makes this possible — and in fact very easy to do. 

In the Terraform with imported YAML sample, you can see how to import an external workflows.yaml file into your Terraform definition:

Importing multiple Workflow definition files

Importing the workflow YAML file is a step in the right direction, but in large workflow definitions, you often have a main workflow calling multiple subworkflows. Workflows doesn’t currently support importing or merging workflow and subworkflow definitions. You end up with a single workflow definition file for the main workflow and all the subworkflows. This is not maintainable. 

Ideally, you’d have each subworkflow in its own file and have the main workflow simply refer to them. Thankfully, this is easy to do in Terraform. 

In the Workflows Terraform with multiple external YAMLs sample, you can see how to import an external workflows.yaml file for the main workflow and a subworkflow.yaml file for the subworkflow into your Terraform definition:

This is more maintainable for sure! One minor issue is that all YAMLs do end up getting merged and deployed as a single YAM to Workflows. When you debug your workflows and subworkflows, you might get confused with line numbers of your subworkflows. 


This wraps up our discussion of Workflows and Terraform. You can check out our workflows-demos repo for all the source code for Terraform samples and more. Thanks to Jamie Thomson for the template file idea on Terraform. Please reach out to me on Twitter @meteatamel for any questions or feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *