Create GCP VM instance using Terraform

Sanket Wadekar
2 min readMay 31, 2021

Terraform is an open-source infrastructure as code software tool created by HashiCorp. Terraform is a tool for deploying resources on cloud platforms. Terraform supports GCP, AWS, Azure, K8S, etc. Let’s learn about how to deploy Virtual Machine in GCP using terraform.

Open google cloud shell and install terraform v0.14 using the following command :

wget https://releases.hashicorp.com/terraform/0.14.0/terraform_0.14.0_linux_amd64.zip

Now unzip the downloaded file by using “unzip FILENAME” command.

Move the executable file into local bin folder using the following command :

sudo mv terraform /usr/local/bin

Run “terraform — version” command to ensure that you have terraform v0.14 installed.

Create main.tf file and add the following content to it.

resource "google_compute_instance" "instance-1" {
name = var.instance-1
machine_type = var.machine-type
zone = var.zone
boot_disk {
initialize_params {
image = var.image
}
}
network_interface {
network = var.network
}
}

Here, we are deploying VM instance but avoided hard-coding parameters in the main.tf file. We are going to use variables.tf file to declare and define the values of these variables. Create variables.tf file and add the following contents to it.

variable instance-1 {
type = "string"
description = "Name of the VM instance"
default = "test-instance-1"
}
variable machine-type {
type = "string"
description = "Name of the machine-type of VM instance"
default = "f1-micro"
}
variable zone {
type = "string"
description = "Name of the zone of the VM instance"
default = "us-central1-a"
}
variable image {
type = "string"
description = "Name of the image used for VM instance"
default = "debian-cloud/debian-9"
}
variable network {
type = "string"
description = "Name of the image used for VM instance"
default = "default"
}

The input variables are declared using variable block. The “type” argument specifies what value types are accepted for the variable. “description” specifies the input variable’s documentation. The “default” argument specifies the default value of the variable.

As we have specified in the variables.tf file, we are creating VM instance with the following configurations :

Name : test-instance-1

Machine-type : f1-micro

Zone : us-central1-a

Image : debian9

VPC : default

Follow the following steps to create VM instance using the created .tf files.

  1. Run the “terraform init” command. This command is used to initialize a working directory containing terraform configuration files.
  2. Run the “terraform plan” command to get the execution plan. We can check the resources that will be created using this command. Although this command alone will not actually carry out the proposed changes.
  3. Run the “terraform apply” command to execute the actions proposed in terraform plan. This command will also show the execution plan and you need to type “yes” at the confirmation prompt to proceed and execute the actions proposed.

“terraform apply” command will take some time to execute as in backend terraform creates VM instance and waits for it to be available.

After executing all these steps, the VM instance will be created in GCP.

--

--