How to Use Terraform to Create a Virtual Machine in Google Cloud Platform

Reading Time: 5 minutes

Hello Readers! In this blog we we are going to see How to set up your first Virtual Machine on Google Cloud platform using Terraform.

Prerequisite:

  1. Installing Terraform
  2. Google Cloud Platform (GCP) Account

Step1. Create a project on GCP (Google Cloud Platform)

I am assuming you already have GCP account, if not then I would suggest you can create your first GCP account.

Alright so to begin with the first thing which we are going to do is Create Project on GCP. Click on NEW PROJECT

I have created a project with the name gcp-terraform-project.

terraform-gcp

Step2. Create a Service account for the project

In the second step, we need to create the Service Account inside the project which we created on the above steps.

Before creating the service account make sure to select the Project which you created.

Head over to left navigation and select IAM & Admin -> Service Account.

gcp-iam & admin

It will redirect you to Service Account to create the page.

Now you need to input some meaningful name for the service account. In my case, I have kept the name my-first-service-account.

service account-gcp

Step3. Assign additional roles to the Service Account

1. Add Project –> Owner role

Now you have created your service account and in the third step we are going to create our first role for the service account.

In the roles drop down look for Project and inside project add Owner role.

Project -> Owner

gcp-role

2. Add Compute –> Compute admin role

The next role which we need is Compute Admin Role available inside Compute

Compute Engine –> Compute Admin Role

gcp-compute-role

3. Add Compute –> Compute Network Admin

The third role which you need to add is Compute Network Admin available inside Compute

Compute Engine –> Compute Network Admin

gcp-compute-network-admin

Step4. Generate Keys for Service Account

After adding the roles now we need to generate the keys for the authorization. We are going to use these keys from our Terraform module later.

From the service account list select the account which you have created (In my case the service account name is my-first-service-account and then click on the 3 dots options.

service-account-gcp

Now you need to look for the manage keys options:

service-account-gcp

Under the Keys section click on ADD KEY -> Create New Key

gcp-key

In the next option, you need to select the type of key. For the current example, we are going to select the JSON.

Now you need to download the key and save it somewhere onto your local computer. (It would be nice if you could rename the file JSON keys file, I have renamed it – gcp-account.json but you can choose any name of your choice).

Step5. Prepare terraform file “main.tf”

Now we are going to write our first Terraform script. Create a file named main.tf.

Firstly we will create one directory after that we will create one main.tf file using these commands.

mkdir gcp-terraform-sample

cd gcp-terraform-sample/

vi main.tf

1. Add the provider details

As you know we are going to provision the virtual machine on Google, so we need to select the provider as google.

 provider "google" {
     credentials = file("gcp-account.json")
     project     = "gcp-terraform-project-353408"
     region      = "europe-west4"
     zone        = "europe-west4-a"
}

Few points you have need to know-

  1. gcp-account.json – It is the JSON key file that we have downloaded in Step 4 . Please save the gcp-account.json at the same location where you have created main.tf
  2. project – You need to mention the Project ID from your google console
  3. region && zone – Select the region and zone which is near your current location.

2. Add “google_compute_instance” instance

Since we aim to create a Virtual Machine on Google Cloud Platform, so we need to add google_compute_instance configuration.

resource "google_compute_instance" "default" {
  name         = "test"
  machine_type = "e2-micro"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }
}

Few points you have need to know –

  1. name – You can keep the name of your virtual instance as per your choice
  2. machine_type – I have chosen e2-micro but you can choose from – e2-small, e2-medium, e2-standard-2 etc.
  3. boot_disk – Here you need to mention the host OS and I have opted for – debian-cloud/debian-9
  4. network_interface – This configuration is needed for getting the IP address of the virtual machine.

Step6. Run – terraform init, terraform plan, terraform apply

Now we have completed all the pre-requisites needed for provisioning the Virtual Machine(VM) on Google Cloud.

The first command which we are going to run is –

1. terraform init

The first command we need to run is :

terraform init

This command is going to download all the required dependencies based on the provider name mentioned in the main.tf. In the current example the provider name is Google so it is going to install Google’s terraform dependencies onto your laptop.

terraform-init

2. terraform plan

The second command which we are going to run is –

terraform plan 

This command tells you –

  1. How many resources are going to be created
  2. How many resources are going to be destroyed
  3. How many resources are going to change.

As you know this is our first example so the terraform plan is going to create 1 resource for us.

Here is the output of the command:

terraform-plan

3. terraform apply

The final command which we are going to run is terraform apply.

This command is going to install/setup the virtual machine on Google Cloud.

terraform apply
terraform-apply

Step7. Verify your Setup

Now at last we are going to verify it by actually logging into the Google Cloud.

Navigate to Compute Engine -> VM Instances

Now you can check your virtual machine running on your GCP dashboard.

virtual-machine

Congratulations! You have successfully provisioned your first Virtual machine running on Google Cloud using Terraform.

Step8. Destroy Virtual Machine

You can destroy the virtual machine running on Google Cloud using the following command

terraform destroy 

Once you run the terraform destroy command then you should see something similar on your terminal

terraform-destroy

Conclusion:

  1. This guide will help you to getting started with terraform
  2. It will give a very good idea of how to write your first terraform configuration file.
  3. You will also have a very good understanding of how to use – terraform initterraform planterrafrom applyterraform destroy
  4. In the end, you will have a very good understanding of how to use google provider’s dependencies with Terraform,

Thank you for sticking to the end. So, In this blog, we have seen How to Use Terraform to Create a Virtual Machine in Google Cloud Platform. Therefore If you like this blog, please do show your appreciation by giving thumbs-ups and sharing this blog and give me suggestions on how I can improve my future posts to suit your needs. Here is my email:gayatri.singh@knoldus.com

Written by 

I am an enthusiastic , hard-working and determine girl with strong attention to detail and eager to learn about new technologies.

Leave a Reply