Stay organized with collections Save and categorize content based on your preferences.

Create a workflow by 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 Google Cloud CLI.

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

Before you begin

Some of the steps in this document might not work correctly if your organization applies constraints to your Google Cloud environment. In that case, you might not be able to complete tasks like creating public IP addresses or service account keys. If you make a request that returns an error about constraints, see how to Develop applications in a constrained Google Cloud environment.

  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 check if billing is enabled on a project.

  4. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

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

  11. 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.

Note that you can use interpolation for substitutions such as reference variables, attributes of resources, and call functions.

  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:
      resource "google_workflows_workflow" "workflows_example" {
        name            = "sample-workflow"
        region          = "us-central1"
        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
            args:
                url: https://us-central1-workflowsample.cloudfunctions.net/datetime
            result: CurrentDateTime
        - readWikipedia:
            call: http.get
            args:
                url: https://en.wikipedia.org/w/api.php
                query:
                    action: opensearch
                    search: $${CurrentDateTime.body.dayOfTheWeek}
            result: WikiResult
        - returnOutput:
            return: $${WikiResult.body[1]}
      EOF
      
        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 on 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