Terraform scripts are used to create, manage, and delete resources.

Resources are deleted after use.

Prerequisites

Get the Terraform scripts from the public-cloud-automation project. These scripts are applicable to Terraform version v0.12.16.

Installing Terraform

  1. Download Terraform: Terraform Download Site.
  2. Install Terraform.
  3. Set the Terraform path in the environmental variables.

To verify that Terraform is installed, type terraform at the command line to display an options list.

Creating and managing resources

To create and manage the resources:

  • Ensure that the Terraform scripts are in place.
  • Download the .json file for the service account and add it to the variable_params.tf file.
  • Ensure that project name and zone are specified.
  • Ensure that the provider.tf file has access to the project.
  • Place all of the required .tf files into the same folder.

If you need to create a set of resources, put all those resources .tf files in the same folder as the provider.tf and variables.tf files.

Commands

The following Terraform commands are used to create, manage, and delete resources:

terraform init  - Initializes Terraform.
terraform plan  - Shows the list of actions.
terraform plan -out=plan_store - Stores and directly applies the plan.
terraform apply plan_store - Stores a plan in the plan_store.
terraform apply - Shows the list of actions and asks for permission to apply the plan actions. 
terraform destroy - Destroys all the resources created with Terraform.
  • By storing a plan in the plan_store, the permission query is bypassed when applying the plan.
  • When a folder contains all types of .tf files, only a specific type of .tf file should be executed.

To execute a single type file, use the following commands:

terraform init
terraform plan -out=plan_store -target="resource_name.logical_name"

For example, to create only instances, use the following commands:

terraform plan -out=plan_store -target="google_compute_instance.instanceTest"
terraform apply plan_store

Resource list

Instances
Cloud SQL Instance
Container Cluster
Topic
Subscription
Router
Storage
cloud DNS
GOOGLE/BigQuery Dataset
GOOGLE/Dataproc Cluster
GOOGLE/Cloud Spanner
BigTable
CloudTask
CloudRun
Composer
Filestore
Redis
TPU
VPN

External reference