A step by step guide to set up Terraform to work with Google Cloud Platform

Infrastructure as Code (IaC) has become a fundamental practice in modern cloud computing. It allows you to define and manage your infrastructure using code, enhancing automation, scalability, and reproducibility. Terraform, an open-source IaC tool developed by HashiCorp, is particularly popular for its flexibility and multi-cloud support. In this step-by-step guide, we will walk you through the process of setting up Terraform with Google Cloud Platform (GCP), enabling you to manage your GCP resources using code.

If you are new to Terraform, Please check out this article to understand Terraform properly.

Step 1: Prerequisites

Before diving into the Terraform setup, make sure you have the following prerequisites in place:

  • Google Cloud Platform Account: You should have a GCP account with billing enabled.
  • Google Cloud SDK: Install the Google Cloud SDK, which includes the gcloud command-line tool, for managing GCP resources.
  • Terraform: Download and install Terraform from the official Terraform website (https://www.terraform.io/downloads.html) based on your operating system.

Step 2: Google Cloud Platform Authentication

To interact with GCP using Terraform, you need to set up authentication. Here’s how:

  • Create a Service Account:
    • Go to the GCP Console.
    • Navigate to IAM & Admin > Service accounts.
    • Create a new service account and assign the necessary roles (e.g., Compute Engine Admin, Storage Admin) based on your requirements.
    • Generate a JSON key file for the service account and Save it locally.
  • Set the JSON file:
    Copy the JSON file in the same directory where you are going to write terraform files. If you want to keep the JSON file in another directory, you can do that, you just need to give the path of the JSON file while creating the terraform file.

Step 3: Initialize Terraform

Navigate to the directory where you want to work with Terraform and create a new directory for your project. Inside your project directory, create a new file named main.tf. This file will contain your Terraform configurations.

Step 4: Configure the GCP Provider

In your main.tf file, specify the GCP provider and other required settings:

provider "google" {
  credentials   = file(credentials_file)          # path for JSON file
  project 	= var.project_id         	  # Your GCP Project ID
  region  	= "us-central1"                   # Your preferred region
}

This configuration tells Terraform to use the GCP provider and authenticate using your service account credentials.

Step 5: Initialize Terraform

In your project directory, run the following command to initialize Terraform:

terraform init

Terraform will download the necessary provider plugins and initialize your working directory.

Step 6: Create Terraform Resources

Now, you can start defining your infrastructure as code using Terraform configurations. For example, you can create a simple Google Cloud Storage bucket in main.tf

resource "google_storage_bucket" "my_bucket" {
  name 	= "my-unique-bucket-name"
  location = "US"
}

Step 7: Plan and Apply

This command generates an execution plan, showing you what Terraform will do.

terraform plan

Apply the configuration to create the resources.

terraform apply

Step 8: Verify Resources

Check your GCP Console to verify that the resources have been created as expected.

Step 9: Manage Resources

You can now manage your resources using Terraform. To make changes to your infrastructure, simply update your Terraform configuration and run Terraform apply again. Terraform will update the infrastructure based on the terraform state file.
So, What is terraform state file?
Terraform state file keeps the current state of the infrastructure. So whenever you want to modify the infrastructure and run the terraform file, terraform will check the desired state(for ex. main.tf) with the current state(which is mentioned in the state file, which will automatically be generated when you run terraform file first time), and based on that it will update the infrastructure.

Step 10: Destroy Resources

When you’re done with your resources, use the terraform destroy command to remove them:

terraform destroy

That’s all for now.
Thank you for reading!!
Stay tuned for more articles on Cloud computing and DevOps. Don’t forget to follow me for regular updates and insights.

Let’s Connect: LinkedIn

1 thought on “A step by step guide to set up Terraform to work with Google Cloud Platform”

  1. Pingback: Terraform Tutorial - Getting Started with Terraform - The CloudOps

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top