Umesh Chandra
Umesh Chandra
Devops Engineer

Getting Started with Terraform on Google Cloud Platform (GCP)

Getting Started with Terraform on Google Cloud Platform (GCP)

Building Infrastructure on GCP using Terraform

In this blog, we will walk through the steps required to set up infrastructure on Google Cloud Platform (GCP) using Terraform.

✅ Prerequisites

Before we begin, make sure the following tools are installed on your system:

Google Cloud CLI (gcloud)

Check version:

1
gcloud --version

Terraform v1.2+

Check version:

1
terraform --version

🛠️ Step-by-Step Setup

1. Create a GCP Account and Project

If you haven’t already, create a GCP account.

Create a new project in GCP and note the project ID. This will be used in your Terraform configuration.

2. Enable the Compute Engine API

Navigate to APIs & Services → Enable APIs.

Search for Compute Engine API and enable it.

Note: This step must be repeated for each new project you create.

3. Authenticate gcloud

After installing the Google Cloud CLI, authenticate using:

1
gcloud auth application-default login

You’ll receive a URL and be prompted for a verification code.

Login with the same Google account used for your GCP project.

Paste the verification code back in the terminal to complete authentication.

4. Set Quota Project (Optional)

To track usage and receive budget warnings, you can set a quota project:

1
gcloud auth application-default set-quota-project <PROJECT_ID>

Example:

1
gcloud auth application-default set-quota-project learn-terraform-gcp-109795

📄 Writing Your First Terraform Configuration

Create a file named main.tf with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "6.8.0"
    }
  }
}

provider "google" {
  project = "<PROJECT_ID>"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}

🔍 Breakdown

terraform block specifies the required providers.

hashicorp/google refers to registry.terraform.io/hashicorp/google.

provider block tells Terraform which provider to use and configures it with project ID, region, and zone.

resource block defines the resource to create — in this case, a VPC network.

1
Format: resource "<TYPE>" "<NAME>"

⚙️ Running Terraform Commands

Once your main.tf is ready, run the following commands:

1. Initialize the Terraform project

1
terraform init

This downloads the necessary provider plugins.

2. Check Terraform format

1
terraform fmt

Formats your configuration files according to standard practices.

3. Validate the configuration

1
terraform validate

Ensures your configuration is syntactically valid.

4. Apply the configuration

1
terraform apply

Terraform will show an execution plan, with + signs indicating resources that will be added.

Type yes to confirm.

🧾 Verification

Once the apply is complete:

Go to the GCP Console

Navigate to VPC Network under Networking

You should see a new network named terraform-network listed

🎉 Conclusion

You’ve now successfully created a VPC network on GCP using Terraform. This setup is the foundation for provisioning more complex infrastructure on Google Cloud. Stay tuned for more!

comments powered by Disqus