Quickstart: Create a workflow using Terraform

This quickstart shows you how to create, deploy, and execute your first workflow using Terraform. Terraform is a HashiCorp open source tool that enables you to predictably create, change, and improve your cloud infrastructure by using code. In this quickstart, the workflow sends a request to a sample API and then uses the response to create and send a request to a public API. The workflow then returns the public API's response.

In this document, using Terraform, you will:

  1. Enable the Workflows API.
  2. Create a service account for the workflow.
  3. Define and deploy a workflow using Terraform.
  4. Execute the workflow with gcloud command-line tool.

For resources and guidance on using Terraform, see Use Terraform with Google Cloud.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Install and initialize the Cloud SDK.
  5. In the Cloud Console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Cloud Console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Cloud SDK already installed, including the gcloud command-line tool, and with values already set for your current project. It can take a few seconds for the session to initialize.

  6. Cloud Shell has Terraform already integrated.

Create a Terraform configuration file

Create a terraform configuration file called main.tf to include the Google Cloud resources used in this project.

  1. Create a directory:
    mkdir terraform
  2. Go to the terraform directory:
    cd terraform
  3. Add a new file, main.tf, to the directory:
    nano main.tf
  4. Add the following resources to the main.tf file:
    1. Assign the ID of the project:
      provider "google" {
       project = "PROJECT_ID"

      Replace PROJECT_ID with your project's ID.

    2. Enable the Workflows API:
      resource "google_project_service" "workflows" {
        service            = "workflows.googleapis.com"
        disable_on_destroy = false
    3. Create a service account for the workflow:
      resource "google_service_account" "workflows_service_account" {
        account_id   = "sample-workflows-sa"
        display_name = "Sample Workflows Service Account"
    4. Define and deploy the workflow, replacing var.region with "us-central1":
      resource "google_workflows_workflow" "workflows_example" {
        name            = "sample-workflow"
        region          = var.region
        description     = "A sample workflow"
        service_account = google_service_account.workflows_service_account.id
        source_contents = <<-EOF
        # This is a sample workflow, feel free to replace it with your source code
        # This workflow does the following:
        # - reads current time and date information from an external API and stores
        #   the response in CurrentDateTime variable
        # - retrieves a list of Wikipedia articles related to the day of the week
        #   from CurrentDateTime
        # - returns the list of articles as an output of the workflow
        # FYI, In terraform you need to escape the $$ or it will cause errors.
        - getCurrentTime:
            call: http.get
                url: https://us-central1-workflowsample.cloudfunctions.net/datetime
            result: CurrentDateTime
        - readWikipedia:
            call: http.get
                url: https://en.wikipedia.org/w/api.php
                    action: opensearch
                    search: $${CurrentDateTime.body.dayOfTheWeek}
            result: WikiResult
        - returnOutput:
            return: $${WikiResult.body[1]}
        depends_on = [google_project_service.workflows]

Deploy and execute the workflow

Deploy your resources with Terraform to create a workflow.

  1. Initialize Terraform in the directory:

    terraform init
  2. Check that the changes you propose with Terraform match the expected plan:
    terraform plan

    You can ignore the note regarding not using the -out option.

  3. Create the workflow:

    terraform apply
  4. At the Enter a value prompt, enter yes to proceed with the creation of resources.
  5. Confirm that a workflow is created:
    gcloud workflows list --location us-central1

    The output should be similar to the following:

    NAME                                                                          STATE   REVISION_ID  UPDATE_TIME
    projects/project-name/locations/us-central1/workflows/sample-workflow         ACTIVE  000001-f9a   2021-06-24T13:38:58.353765906Z

  6. Optionally, you can execute the workflow:
    gcloud workflows execute sample-workflow

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this page, follow these steps.

You can delete all the resources you created with Terraform using:

terraform destroy

You can also delete the workflow you created.

  1. Delete the workflow you created:
    gcloud workflows delete sample-workflow
  2. When asked if you want to continue, enter y.

The workflow is deleted.

What's next