Terraform language
As mentioned, the Terraform language is based on HCL. The most basic construct is a block, defined as a “container for other content” (https://developer.hashicorp.com/terraform/language/syntax/configuration#blocks). For example, the preceding code block is of the type resource
. A resource block has two labels. The first label consists of the provider and resource name. For example, the resource label for a Google compute engine is google_compute_instance
, whereas the label for an AWS E2 instance is aws_instance
. The second label in a resource block is the ID, the name you give so that Terraform can uniquely identify each resource. Please note that no two resources can have the same name. So, if you want to provision two VMs, you define two resource blocks, each of type google_compute_instance
, and each with a unique name.
The body of the block is nested within {}
. Blocks can have nested blocks. In the preceding code example, there are two nested blocks, boot_disk
(which in itself has a nested block called initialize_params
) and network_interface
.
While the syntax for Terraform is the same, each cloud provider has its unique resources and data source definitions. The Terraform documentation for Google Cloud is at https://registry.terraform.io/providers/hashicorp/google/latest/docs. You see that all resources and data sources start with the google
keyword. In general, it is followed by the name of the equivalent gcloud
command. Thus, the resource to create a compute instance is named google_compute_instance
, whereas the resource to provision a SQL database instance is named google_sql_database_instance
.
Looking at the documentation, you can see the current Google services that are currently supported. More are added as Google Cloud introduces new services. We recommend you bookmark the documentation as you have to refer to it frequently.
In the first part of this book, we use only a few Google Cloud resources as we want to concentrate on the language. We design complex architectures using additional Google Cloud services in the second part. But first, let’s look at the steps involved in using Terraform.