How to integrate Terraform with GCP

Terraform is an open-source tool for infrastructure as code (IaC). It allows users to describe and set up data center infrastructure using a language known as HashiCorp Configuration Language (HCL) or JSON.

Google Cloud Platform (GCP) provides many cloud computing services, including computing, storage, and networking. The integration of Terraform with GCP facilitates the efficient management and deployment of cloud resources in a declarative and automated fashion. This Answer outlines the steps required to integrate Terraform with GCP.

Prerequisites

Before initiating the integration process, it is essential to verify that the following prerequisites must be fulfilled:

  1. We must possess a Google Cloud Platform (GCP) account with sufficient permissions to generate and administer resources.

  2. Terraform has been successfully installed on our local machine. We can download Terraform from the official websitehttps://developer.hashicorp.com/terraform/install?product_intent=terraform and follow the installation guidelines.

Steps to integrate Terraform with GCP

Establish GCP Authentication: An authentication setup is necessary to interact with GCP resources through Terraform. Follow these steps:

  1. Access the Google Cloud Console and locate the IAM & Admin section.

  2. Create a service account specifically for Terraform or utilize an existing one.

  3. Generate a JSON key associated with the service account and ensure its secure storage on our local machine.

  4. Define the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the JSON key file’s path.

Creating a Terraform configuration file: Let’s create a simple Terraform configuration file (.tf) to define the state of GCP resources. Below is a basic example demonstrating the creation of a Google Cloud Platform (GCP) Compute Engine instance:

provider "google" {
project = "<your-project-id>"
region = "us-central1" # Replace with your desired region
}
resource "google_compute_instance" "example_instance" {
name = "my-instance"
machine_type = "n1-standard-1"
zone = "us-central1-a" # Replace with your desired region
boot_disk {
initialize_params {
image = "debian-cloud/debian-10" # Replace with your desired OS image
}
}
network_interface {
network = "default"
access_config {
}
}
}

Terraform maintains a registryhttps://registry.terraform.io/providers/hashicorp/google/latest/docs comprising code snippets written in the official HashiCorp Configuration Language (HCL) for creating various Google Cloud Platform (GCP) resources.

Note: Make sure to replace "your-project-id" with your GCP project ID.

Initialize Terraform: Navigate to the directory where the Terraform configuration file is located. Then, execute the following command to initiate Terraform and acquire the necessary provider plugins:

terraform init

Plan and apply changes: After initializing Terraform, we can review the upcoming changes by utilizing the terraform plan command.

terraform plan

Review the proposed modifications, and if they meet our requirements, proceed to implement them by executing the following command:

terraform apply

When utilizing Terraform, we’ll be prompted to confirm any impending changes before execution. Simply type “yes” to proceed with the changes.

After the changes have been applied, verifying that the resources have been created as anticipated is crucial. We can do this by inspecting the GCP Console or utilizing the command-line tools available from GCP.

Conclusion

Combining Terraform with Google Cloud Platform streamlines the management and setup of cloud resources in an automated and repeatable way. By following the steps provided in this guide, we can efficiently utilize Terraform to establish, deploy, and oversee infrastructure on GCP. Feel free to explore various resource types and configurations to meet our unique needs.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved