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:
- Installing Terraform
- 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.

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.



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.



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



2. Add Compute –> Compute admin role
The next role which we need is Compute Admin Role available inside Compute
Compute Engine –> Compute Admin 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



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.



Now you need to look for the manage keys options:



Under the Keys section click on ADD KEY -> Create New 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-
- 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
- project – You need to mention the Project ID from your google console
- 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 –
- name – You can keep the name of your virtual instance as per your choice
- machine_type – I have chosen e2-micro but you can choose from – e2-small, e2-medium, e2-standard-2 etc.
- boot_disk – Here you need to mention the host OS and I have opted for –
debian-cloud/debian-9
- 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.



2. terraform plan
The second command which we are going to run is –
terraform plan
This command tells you –
- How many resources are going to be created
- How many resources are going to be destroyed
- 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:



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



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 I
nstances
Now you can check your virtual machine running on your GCP dashboard.



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



Conclusion:
- This guide will help you to getting started with terraform
- It will give a very good idea of how to write your first terraform configuration file.
- You will also have a very good understanding of how to use –
terraform init
,terraform plan
,terrafrom apply
,terraform destroy
- 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


