0
0
GCPcloud~30 mins

Terraform GCP provider setup - Mini Project: Build & Apply

Choose your learning style9 modes available
Terraform GCP provider setup
📖 Scenario: You are starting a new cloud project on Google Cloud Platform (GCP). To manage your cloud resources efficiently, you want to use Terraform, a tool that helps you write code to create and manage cloud infrastructure.Before you can create any resources, you need to set up Terraform to connect to your GCP account properly.
🎯 Goal: Set up Terraform configuration to use the GCP provider with the correct project and region settings.
📋 What You'll Learn
Create a Terraform configuration file named main.tf.
Configure the GCP provider with the exact project ID my-gcp-project-123.
Set the region to us-central1.
Specify the provider version as 4.0.0.
Add a variable for the project ID with a default value.
💡 Why This Matters
🌍 Real World
Terraform is widely used to manage cloud infrastructure as code. Setting up the provider correctly is the first step to automate resource creation and management on GCP.
💼 Career
Cloud engineers and DevOps professionals must know how to configure Terraform providers to work with cloud platforms like GCP for efficient infrastructure automation.
Progress0 / 4 steps
1
Create Terraform configuration file with provider block
Create a file named main.tf and write a terraform block with required providers specifying google = { source = "hashicorp/google", version = "4.0.0" }. Then add a provider block for google with project = "my-gcp-project-123" and region = "us-central1".
GCP
Need a hint?

Start by defining the terraform block with required_providers. Then add the provider "google" block with the project and region.

2
Add a variable for the project ID
Add a Terraform variable block named project_id with a default value set to "my-gcp-project-123".
GCP
Need a hint?

Use the variable keyword followed by the variable name and set the default value inside curly braces.

3
Use the variable in the provider block
Modify the provider "google" block to use the variable project_id for the project attribute by setting project = var.project_id.
GCP
Need a hint?

Replace the fixed project string with var.project_id inside the provider block.

4
Add a backend configuration for remote state
Add a terraform backend block inside the existing terraform block to configure remote state storage using gcs with bucket = "my-terraform-state-bucket" and prefix = "terraform/state".
GCP
Need a hint?

Inside the terraform block, add a backend "gcs" block with the bucket and prefix settings.