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:
- Enable the Workflows API.
- Create a service account for the workflow.
- Define and deploy a workflow using Terraform.
- Execute the workflow with
gcloudcommand-line tool.
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.
- 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.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Install and initialize the Cloud SDK.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Install and initialize the Cloud SDK.
-
In the Cloud Console, 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
gcloudcommand-line tool, and with values already set for your current project. It can take a few seconds for the session to initialize.
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.
- Create a directory:
mkdir terraform
- Go to the
terraformdirectory:cd terraform
- Add a new file,
main.tf, to the directory:nano main.tf
- Add the following resources to the
main.tffile:- Assign the ID of the project:
provider "google" { project = "PROJECT_ID" }Replace
PROJECT_IDwith your project's ID. - Enable the Workflows API:
- Create a service account for the workflow:
- Define and deploy the workflow:
- Assign the ID of the project:
Deploy and execute the workflow
Deploy your resources with Terraform to create a workflow.
- Initialize Terraform in the directory:
terraform init
- Check that the changes you propose with Terraform match the
expected plan:
terraform plan
You can ignore the note regarding not using the
-outoption. - Create the workflow:
terraform apply
- At the Enter a value prompt, enter
yesto proceed with the creation of resources. - 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
- 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.
- Delete the workflow you created:
gcloud workflows delete sample-workflow
- When asked if you want to continue, enter
y.
The workflow is deleted.

