Mastering GCP Infrastructure with Terraform: Regional HTTPS Load Balancer with Cloud DNS - Part 4
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 the final installment of our series, "Mastering GCP Infrastructure with Terraform: Regional HTTPS Load Balancer with Cloud DNS." In Part 3, we built a robust backend infrastructure with Instance Templates, Managed Instance Groups (MIGs), health checks, and autoscaling.
In Part 4, we’ll bring everything together by setting up a Regional HTTPS Load Balancer and integrating Cloud DNS. This ensures secure and reliable routing of traffic to your backend infrastructure. Additionally, we’ll configure HTTP-to-HTTPS redirection and a Cloud NAT for outbound internet access.
By the end of this part, your infrastructure will be fully operational, scalable, and production-ready.
Setting Up the Load Balancer
The load balancer is the heart of our infrastructure. It distributes incoming traffic to backend instances securely and efficiently. Let’s go step by step.
1. Static IP for the Load Balancer
resource "google_compute_address" "mylb" {
name = "${local.name}-mylb-regional-static-ip"
region = var.gcp_region1
}
2. Backend Service and Health Check
resource "google_compute_region_health_check" "mylb" {
name = "${local.name}-mylb-myapp1-health-check"
check_interval_sec = 5
timeout_sec = 5
healthy_threshold = 2
unhealthy_threshold = 2
http_health_check {
request_path = "/index.html"
port = 80
}
}
resource "google_compute_region_backend_service" "mylb" {
name = "${local.name}-myapp1-backend-service"
protocol = "HTTP"
load_balancing_scheme = "EXTERNAL_MANAGED"
health_checks = [google_compute_region_health_check.mylb.self_link]
port_name = "webserver"
backend {
group = google_compute_region_instance_group_manager.myapp1.instance_group
capacity_scaler = 1.0
balancing_mode = "UTILIZATION"
}
}
Health Check: Ensures the backend service routes traffic only to healthy instances.
Backend Service:
3. URL Mapping
resource "google_compute_region_url_map" "mylb" {
name = "${local.name}-mylb-url-map"
default_service = google_compute_region_backend_service.mylb.self_link
}
4. HTTPS Proxy and Forwarding Rule
resource "google_compute_region_target_https_proxy" "mylb" {
name = "${local.name}-mylb-https-proxy"
url_map = google_compute_region_url_map.mylb.self_link
certificate_manager_certificates = [google_certificate_manager_certificate.myapp1.id]
}
resource "google_compute_forwarding_rule" "mylb" {
name = "${local.name}-mylb-forwarding-rule"
target = google_compute_region_target_https_proxy.mylb.self_link
port_range = "443"
ip_protocol = "TCP"
ip_address = google_compute_address.mylb.address
load_balancing_scheme = "EXTERNAL_MANAGED"
}
HTTPS Proxy:
Forwarding Rule:
领英推荐
5. HTTP-to-HTTPS Redirection
resource "google_compute_region_url_map" "http" {
name = "${local.name}-myapp1-http-to-https-url-map"
default_url_redirect {
redirect_response_code = "MOVED_PERMANENTLY_DEFAULT"
strip_query = false
https_redirect = true
}
}
resource "google_compute_region_target_http_proxy" "http" {
name = "${local.name}-myapp1-http-to-https-proxy"
url_map = google_compute_region_url_map.http.self_link
}
resource "google_compute_forwarding_rule" "http" {
name = "${local.name}-myapp1-http-to-https-forwarding-rule"
target = google_compute_region_target_http_proxy.http.self_link
port_range = "80"
ip_protocol = "TCP"
ip_address = google_compute_address.mylb.address
load_balancing_scheme = "EXTERNAL_MANAGED"
}
URL Map for HTTP:
Forwarding Rule for HTTP:
Adding Cloud DNS
locals {
mydomain = "myapp1.rezaops.com"
dns_managed_zone = "rezaopscom"
}
resource "google_dns_record_set" "a_record" {
managed_zone = local.dns_managed_zone
name = "${local.mydomain}."
type = "A"
ttl = 300
rrdatas = [google_compute_address.mylb.address]
}
Cloud DNS:
Final Outputs
output "mylb_static_ip_address" {
description = "The static IP address of the load balancer."
value = google_compute_address.mylb.address
}
output "mylb_target_https_proxy_self_link" {
description = "The self-link of the target HTTPS proxy."
value = google_compute_region_target_https_proxy.mylb.self_link
}
Purpose:
Why These Steps Matter
Conclusion
?? Congratulations! You’ve now completed the infrastructure setup for your Regional HTTPS Load Balancer with Cloud DNS in GCP. Let’s recap:
Your infrastructure is now production-ready, scalable, and secure.