GCP | How to create VM in GCP with Terraform

Learn how to create a VM in GCP with Terraform.

Terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Learn more about terraform at https://developer.hashicorp.com/terraform.

Setting up terraform for GCP

If you don't have Terraform set up ready, refer Terraform set-up for GCP to complete the set-up.

Create a GCP VM with terraform

1. Create new file createvm.tf and write below code in the file.

   
  resource "google_compute_instance" "vm_instance" {
    name         = "gcptutorials-vm"
    machine_type = "f1-micro"
  
    boot_disk {
      initialize_params {
        image = "debian-cloud/debian-9"
      }
    }    
    network_interface {       
      network = "default"
      access_config {
      }
    }
  }
 

2. Open the terminal and run the terraform fmt command to format terraform files.

 

  terraform fmt
   

3. Run terraform init command to initializes working directory containing Terraform configuration files.

   

  terraform init
   

4. Run terraform plan command to check execution plan.

   

  terraform plan
   

5. Run terraform apply command to create VM in GCP.

   

  terraform apply
   

6. After performing all the steps you should see a VM with name gcptutorials-vm in GCP.


Category: GCP