8/12/2024 • VirtueCloud

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define, provision, and manage infrastructure across various cloud providers using a declarative configuration language. In this blog, we will provide a comprehensive guide to setting up Terraform for Google Cloud Platform (GCP) and a hands-on tutorial to provision infrastructure on GCP.
Google Cloud Platform (GCP) is a suite of cloud computing services provided by Google. It offers a wide range of cloud services, including computing, storage, data analytics, and machine learning, which run on the same infrastructure that Google uses internally for its end-user products like Google Search, Gmail, and YouTube. This blog will guide you through setting up Terraform for GCP and provide a hands-on tutorial to provision infrastructure on GCP.
Before you begin, ensure you have the following:
Google Cloud Platform (GCP) is a suite of cloud computing services provided by Google. It offers a wide range of cloud services, including computing, storage, data analytics, and machine learning. Here are some key components of GCP:

By understanding these components, you can better appreciate the versatility and power of GCP in meeting various infrastructure and application needs. In this tutorial, we will focus on Compute Engine to demonstrate how to deploy infrastructure using Terraform.
1. Create a GCP Project
2. Enable Required APIs
3. Create a Service Account
Open Terminal and Run: terraform --version
Ensure you see the version number to confirm that Terraform is installed correctly. If not installed click here and follow the terraform install guide.
Create Terraform Configuration Files
Create a Directory for Terraform Files:
mkdir terraform-gcp
cd terraform-gcp Inside this directory create a filed named main.tf:
provider "google"{
credentials = file(var.credentials)
project = var.project
region = var.region
}
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "f1-micro"
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
access_config {
}
}
service_account {
email = var.service_account_email
scopes = ["cloud-platform"]
}
} Create variables.tf :
variable "credentials" {
description = "The path to the service account key file"
type = string
default = "C:/Users/ABC/Terraform/GCP/Service_Acc_Key.json" #<path-to-your-service-account-key>"
}
variable "project" {
description = "The GCP project ID"
type = string
default = "<your-gcp-project-id>"
}
variable "region" {
description = "The GCP region"
type = string
default = "asia-south1"
}
variable "zone" {
description = "The GCP zone"
type = string
default = "asia-south1-a"
}
variable "service_account_email" {
description = "Service Account Email"
type = string
default = "<your-service-account-email-address>"
} Let's initialize the working directory :
terraform init
Once it is initialized , run :
terraform plan
It allows you to preview the changes terraform will make before you apply them.
After making sure the plan is accordingly correct, apply the changes by :
terraform apply
Confirm the apply by typing yes.
Navigate to the Compute Engine section in the GCP Console to see your newly created instance. This is similar to checking the EC2 Dashboard in the AWS Management Console to verify your EC2 instance.
In this blog, we walked through the process of setting up Terraform for GCP and provisioning a basic Compute Engine instance. Terraform's declarative approach to infrastructure management simplifies the process of defining, provisioning, and managing cloud resources. Drawing parallels between GCP and AWS can help those familiar with AWS to transition smoothly to GCP.
Related articles you might find interesting

Why platform teams are re-routing north-south traffic through the Kubernetes Gateway API, what HTTPRoute changes on the ground, and how to migrate without a big-bang rewrite.


Hassle-Free ECS: Terraform Automation + CI/CD Pipeline