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:
- Enable the Workflows API.
- Create a service account for the workflow.
- Define and deploy a workflow using Terraform.
- 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.
- 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 check if billing is enabled on a project.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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 check if billing is enabled on a project.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, 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.
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.
- Create a directory:
mkdir terraform
- Go to the
terraform
directory:cd terraform
- Add a new file,
main.tf
, to the directory:nano main.tf
- Add the following resources to the
main.tf
file:- Assign the ID of the project:
provider "google" { project = "PROJECT_ID" }
Replace
PROJECT_ID
with 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
-out
option. - Create the workflow:
terraform apply
- At the Enter a value prompt, enter
yes
to 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.