Create a workflow by using Terraform
This quickstart shows you how to create, deploy, and execute your first workflow using Terraform. Terraform is an infrastructure-as-code tool that enables you to predictably create, change, and improve your cloud infrastructure by using code. Learn how to use Terraform to provision infrastructure on Google Cloud.
In this quickstart, the sample workflow sends a request to a public API and then returns the API's response.
You will complete the following:
- Enable the Workflows API using Terraform.
- Create a service account for the workflow using Terraform.
- Define and deploy a workflow using Terraform.
- Execute the workflow using the Google Cloud CLI.
Before you begin
Security constraints defined by your organization might prevent you from completing the following steps. For troubleshooting information, see Develop applications in a constrained Google Cloud environment.
Note that Cloud Shell has Terraform already integrated. If you need to install Terraform, see the HashiCorp Terraform documentation.
- 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.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Identity and Access Management (IAM) APIs:
gcloud services enable cloudresourcemanager.googleapis.com
iam.googleapis.com - Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Identity and Access Management (IAM) APIs:
gcloud services enable cloudresourcemanager.googleapis.com
iam.googleapis.com
Create a Terraform configuration file
Create a Terraform configuration file called main.tf
and include the
Google provider for Terraform resources used in
this quickstart.
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 the workflow using the
google_workflows_workflow
resource:The following arguments are used in the sample workflow:
name
: the name of your workflow.region
: the location of your workflow.description
: a description of your workflow.service_account
: the email address or unique ID of the service account associated with the latest workflow version. This service account represents the identity of the workflow and determines what permissions the workflow has. If you don't specify a service account during the workflow's creation, the workflow uses the default Compute Engine service account for its identity. For more information, see Grant a workflow permission to access Google Cloud resources.labels
: a list of key-value label pairs to assign to this workflow that helps you organize your Google Cloud instances. For more information, see What are labels?user_env_vars
: user-defined environment variables associated with this workflow revision. For more information, see Use environment variables.source_contents
: the Workflows code to execute. For the file size limit, see Resource limits.
Other optional arguments include the following:
crypto_key_name
: the resource ID for a Cloud Key Management Service key in the following format:projects/PROJECT_NAME/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME
For more information, see Use customer-managed encryption keys.
call_log_level
: the level of logging to apply to calls and call responses during executions of this workflow. Possible values are:CALL_LOG_LEVEL_UNSPECIFIED
LOG_ALL_CALLS
LOG_ERRORS_ONLY
LOG_NONE
For more information, see Call logging.
project
: the ID of the project in which the resource belongs. If it is not provided, the provider project is used.name_prefix
: creates a unique name beginning with the specified prefix. If this andname
are unspecified, a random value is chosen for the name.
Create and execute the workflow
Deploy your Terraform resources to create the workflow and then execute the 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, type
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 2024-02-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, delete the Google Cloud project with the resources.
- Delete all the resources you created with Terraform:
terraform destroy
- Delete the workflow you created:
gcloud workflows delete sample-workflow
When asked if you want to continue, entery
. - Alternatively, you can delete your Google Cloud project to avoid incurring
charges. Deleting your Google Cloud project stops billing for all the
resources used within that project.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID