Automating GCP to Launch VM Instance with Terraform

Automating GCP to Launch VM Instance with Terraform

08 December 2020

Terraform is a powerful tool that lets you build, change, and version your infrastructure safely and efficiently. Terraform helps you manage existing, popular service providers like GCP, AWS, as well as custom in-house solutions.


Terraform uses Configuration files to get a description of all the components needed to run one application or your entire datacenter. Terraform generates an execution plan describing what it’ll do to achieve the specified state, then executes it to create the described infrastructure. As the configuration changes, Terraform can see what changed and make incremental execution plans which can be applied further.

Terraform can manage low-level infrastructure components like storage, compute instances, and networking, in addition to high-level components such as SaaS features, DNS entries, etc.

Terraform features

Platform Compatibility:

Terraform comes preinstalled in Google Cloud Shell and a few other public cloud platforms. Hence, We have already skipped the hassle to install terraform on GCP.

Infrastructure as Code:

Infrastructure is described using a high-level configuration syntax which can also be called as a blueprint of your datacenter. This can be versioned, shared and is reusable.

Execution Plans:

Terraform features a “planning” step. Terraform generates an execution plan at this step. The execution plan denotes what Terraform will do once you fire ‘apply’ command. This helps you to avoid any surprises when Terraform manipulates infrastructure.

Resource Graph:

Terraform builds a graph of all of your resources, and enables simultaneous creation and modification of any non-dependent resources. Because of this, operators get insight into dependencies in their infrastructure.

Change Automation:

Complex changesets are often applied to your infrastructure with minimal human interaction. With the previously mentioned execution plan and resource graph, you recognize exactly what changes will be executed by Terraform. This helps to avoid any potential human errors.


The group of files that describe the infrastructure in Terraform is called ‘Terraform configuration’. We’re going to write our first configuration now to launch a single VM instance with the name ‘terraform_instance’.

Now let’s create a file using nano editor. Execute command:


I recommend using JSON for creating configuration files.

Add the following content in the file, Make sure to replace <PROJECT_ID> with your globally unique GCP project ID:

resource "google_compute_instance" "default" { project = "<PROJECT_ID>" name = "terraform_instance" machine_type = "n1-standard-1" zone = "us-central1-a" boot_disk { initialize_params { image = "debian-cloud/debian-9" } } network_interface { network = "default" access_config { } } }

Ctrl + X will exit the nano editor.

This is an entire configuration that Terraform is prepared to use. The general structure should be intuitive and straightforward.

The “resource” block in the file defines a resource that exists within the infrastructure. A resource might be a physical component such as a VM instance.

The resource block has two properties before opening the block: the resource name and the resource type. In our case the name will be “terraform” and the resource type is “google_compute_instance”. The prefix of the type maps to the provider: google_compute_instance automatically tells Terraform that it is managed by the Google provider.

Terraform supports numerous infrastructures and service providers. In order to support such an ecosystem and its codependency, terraform uses plugin-based architecture. Each “Provider” has its own binary which is encapsulated. It is distributed separately from Terraform itself.

The ‘terraform init’ command will automatically download and install any Provider binary for the providers to use within the configuration, which in this case is just the Google provider.

terraform init

To create the execution plan, use the command ‘terraform plan’. Terraform makes a refresh, and then determines what actions are necessary to achieve the desired state specified in the configuration files.

terraform plan

Apply Changes

In the same directory as the file you created, run terraform apply.

terraform apply

This will pause the Terraform and will wait for approval from you before proceeding. In a production environment, if anything in the Execution Plan seems incorrect or dangerous, it’s safe to abort here. No changes will be made to your infrastructure.

Now, Type yes at the confirmation prompt to proceed.

It will take a few minutes for terraform to configure your resources. In our case, the VM instance will be created.

Go to the Navigation Bar on the left side of Google Cloud console and Click on “Compute Engine”. Click on “VM instances”.

You will see that a VM instance with the name terraform_instance” has been successfully created.

Stay tuned to Tudip Technologies blog to keep learning more advanced tutorials on terraform and GCP.

Request a quote