LAB12_GCP_Traffic Routing: Global External Application Load Balancer

Traffic Routing: Global External Application Load Balancer

☁ GCP 🟢 Beginner Load Balancing Backend Service Global Proxy

Scenario

You have an unmanaged group of instances (or an empty backend for right now) and you need to deploy Google's globally distributed External Application Load Balancer. In GCP, unlike AWS ALB which is a single resource block, Load Balancing consists of multiple distinct resources hooked together like Lego pieces: Forwarding Rule → Target Proxy → URL Map → Backend Service → Health Check.

Your Objectives
  • Create a google_compute_health_check on TCP port 80.
  • Create a google_compute_backend_service of load balancing scheme EXTERNAL_MANAGED linking to the health check.
  • Create a google_compute_url_map routing all traffic to that backend.
  • Create a google_compute_target_http_proxy tying to the URL map.
  • Create a google_compute_global_forwarding_rule listening on port 80 attached to the proxy.
  • Output the global IP of the Forwarding Rule.

Additional Context

The Google LB Architecture: Wait, why so many resources? GCP separates concerns allowing immense flexibility.

  • Forwarding Rule: The front door, owns the IP address and port.
  • Target Proxy: Terminates HTTP(S), applies SSL certificates.
  • URL Map: The router ("if path is /api, go to backend X, else go to Y").
  • Backend Service: Manages the pool of compute/serverless instances, handles balancing algorithms (e.g. Rate, Utilization).
In this simple lab, we won't attach an instance group to the backend service (it will return a 502), but we will lay the complete LB track.

variables.tf

variable "project_id" { type = string }

main.tf

provider "google" {
  project = var.project_id
}

# 1. Health Check
resource "google_compute_health_check" "default" {
  name = "lb-health-check"
  
  tcp_health_check {
    port = 80
  }
}

# 2. Backend Service (receives traffic, routes to backends based on health)
resource "google_compute_backend_service" "default" {
  name                  = "web-backend-service"
  protocol              = "HTTP"
  port_name             = "http"
  load_balancing_scheme = "EXTERNAL_MANAGED"
  health_checks         = [google_compute_health_check.default.id]
  
  # Normally, you would add a `backend { group = google_compute_instance_group... }` here.
}

# 3. URL Map (the router/host-path matcher)
resource "google_compute_url_map" "default" {
  name            = "web-url-map"
  default_service = google_compute_backend_service.default.id
}

# 4. Target Proxy (terminates the connection)
resource "google_compute_target_http_proxy" "default" {
  name    = "web-http-proxy"
  url_map = google_compute_url_map.default.id
}

# 5. Global Forwarding Rule (the public IP and port)
resource "google_compute_global_forwarding_rule" "default" {
  name                  = "web-forwarding-rule"
  target                = google_compute_target_http_proxy.default.id
  port_range            = "80"
  load_balancing_scheme = "EXTERNAL_MANAGED"
}

outputs.tf

output "lb_ip_address" {
  value = google_compute_global_forwarding_rule.default.ip_address
}

Workflow Commands

terraform init
terraform apply -var="project_id=my-gcp-project-123"

# Verify the IP address created (will return 502 since no actual backend VMs are attached)
curl -I http://$(terraform output -raw lb_ip_address)

terraform destroy -var="project_id=my-gcp-project-123"
✓ Expected Output After Apply
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

Outputs:

lb_ip_address = "34.120.xxx.xxx"