Deploying GCP via Terraform
×


Deploying GCP via Terraform

183

🚀 Introduction to Deploying GCP via Terraform

Google Cloud Platform (GCP) is one of the leading cloud providers, and Terraform offers a powerful way to automate infrastructure provisioning on GCP. Deploying GCP via Terraform enables you to manage infrastructure as code (IaC), improving repeatability, versioning, and scalability. In this guide, we’ll walk through setting up a basic GCP environment using Terraform.

🔧 Prerequisites

  • Terraform installed on your local machine (terraform -v to check)
  • A Google Cloud Platform account with billing enabled
  • Google Cloud SDK installed and authenticated (gcloud init)
  • A GCP project and access to a service account with proper IAM permissions

📁 Step 1: Create a Terraform Project Directory


mkdir gcp-terraform-setup
cd gcp-terraform-setup

🧾 Step 2: Create Service Account & Credentials

Generate a service account key from your GCP project and download the JSON file. Set the path as an environment variable:


export GOOGLE_CLOUD_KEYFILE_JSON="path/to/your-service-account.json"

📝 Step 3: Terraform Configuration for GCP

Now create a main.tf file with the following content to deploy a simple VM instance:


provider "google" {
  credentials = file(var.credentials_file)
  project     = var.project_id
  region      = var.region
  zone        = var.zone
}

resource "google_compute_instance" "vm_instance" {
  name         = "terraform-gcp-vm"
  machine_type = "e2-medium"
  zone         = var.zone

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

  network_interface {
    network       = "default"
    access_config {
    }
  }
}

🧮 Step 4: Define Variables

Create a variables.tf file to define inputs for credentials, project ID, region, and zone:


variable "credentials_file" {
  description = "Path to GCP service account key file"
  type        = string
}

variable "project_id" {
  description = "GCP project ID"
  type        = string
}

variable "region" {
  description = "GCP region"
  default     = "us-central1"
}

variable "zone" {
  description = "GCP zone"
  default     = "us-central1-a"
}

📤 Step 5: Create a Terraform Variables File

To simplify your workflow, create a terraform.tfvars file:


credentials_file = "path/to/your-service-account.json"
project_id       = "your-gcp-project-id"
region           = "us-central1"
zone             = "us-central1-a"

⚙️ Step 6: Initialize and Apply Terraform

Initialize the working directory and apply the Terraform configuration:


terraform init
terraform plan
terraform apply

Terraform will prompt you to confirm. Type yes to deploy the resources on GCP.

🔍 Verification

Go to the Google Cloud Console > Compute Engine > VM Instances and verify that the VM has been successfully created.

🧹 Cleaning Up

To delete the provisioned resources and avoid incurring costs, use:


terraform destroy

📚 Best Practices for GCP Deployment via Terraform

  • Use remote state storage like Google Cloud Storage (GCS) for collaboration
  • Split configuration into modules for reusability
  • Use Terraform workspaces to manage different environments
  • Keep sensitive values secure using environment variables or Vault

📌 Summary

Deploying GCP via Terraform brings automation, efficiency, and repeatability to your infrastructure management. By writing declarative configuration files and executing them through Terraform, you eliminate manual provisioning and reduce human error. Start small by provisioning a VM, and gradually expand to managing networks, IAM roles, and Kubernetes clusters—all through code.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat