Mastering GCP Infrastructure with Terraform: Regional HTTPS Load Balancer with Cloud DNS - Part 1
Reza Chegini
Certified GCP & AWS DevOps Engineer| Seeking Entry-Level Cloud Developer, DevOps, SRE Roles, Software Engineer or Developer | Aspiring DevOps & SRE
Introduction
?? Welcome to Part 1 of the series, "Mastering GCP Infrastructure with Terraform: Regional HTTPS Load Balancer with Cloud DNS." In this series, I’ll guide you through building a scalable, secure, and reliable web infrastructure in Google Cloud Platform (GCP) using Terraform.
In Part 1, we’ll lay the foundation for our infrastructure. This includes configuring Terraform, setting up the GCP provider, defining variables, and creating reusable local values. These initial steps are critical for ensuring your deployment is dynamic, organized, and easy to manage.
By the end of this part, you’ll have a solid understanding of how to prepare Terraform for the upcoming tasks.
Setting Up the Foundation
Let’s start by preparing the Terraform configuration to interact with GCP and define dynamic values for reusable infrastructure
1. Configuring Terraform and the GCP Provider
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "6.6.0"
}
}
}
provider "google" {
project = "terraform-gcp-project"
region = "us-central1"
}
Explanation:
2. Defining Variables
variable "gcp_project" {
description = "Project where GCP resources will be created."
type = string
default = "terraform-gcp-project"
}
variable "gcp_region1" {
description = "Region for resource deployment."
type = string
default = "us-central1"
}
variable "machine_type" {
description = "Type of Compute Engine machine."
type = string
default = "e2-micro"
}
variable "environment" {
description = "Environment prefix for resources."
type = string
default = "dev"
}
Explanation:
Variables make your Terraform code dynamic and reusable by allowing you to define values externally.
Key Variables:
领英推荐
3. Creating Local Values
locals {
owners = var.environment
environment = var.environment
name = "${var.environment}-${var.gcp_project}"
common_tags = {
environment = local.environment
owner = local.owners
}
}
Explanation:
Local values store reusable expressions to simplify the configuration:
4. Default Values
gcp_project = "terraform-gcp-project"
gcp_region1 = "us-central1"
machine_type = "e2-micro"
environment = "name"
business_divsion = "name"
Explanation of Variables
Why These Steps Matter
What’s Next?
In Part 2, we’ll dive into networking—setting up a custom Virtual Private Cloud (VPC), defining subnets, and configuring firewall rules to control traffic. These networking components will provide a secure and scalable backbone for the rest of the infrastructure.