# External Application Load Balancer A global HTTPS load balancer in front of a Cloud Run service, with a TLS policy, a web application firewall policy and, optionally, Identity-Aware Proxy. The chain is: forwarding rule → target HTTPS proxy → URL map → backend service → serverless network endpoint group → Cloud Run service. Each setting lives on one link of it, and putting one on the wrong link is the usual mistake: - **The minimum TLS version is on an SSL policy, attached to the target HTTPS proxy** (`ssl_policy`). A backend service has no `ssl_policy` setting. - **The certificate is on the target HTTPS proxy** (`ssl_certificates`). - **The firewall policy is attached to the backend service** (`security_policy`). - **Identity-Aware Proxy is the `iap` block of the backend service.** It decides who reaches the application; what a signed-in user may do inside it is the application's own code and cannot be shown by infrastructure. - **Request logging is the `log_config` block of the backend service.** - A backend service for Cloud Run uses a serverless NEG and takes no `health_checks`, no `port_name` and no `timeout_sec`. - `load_balancing_scheme = "EXTERNAL_MANAGED"` on both the backend service and the forwarding rule. - Set the Cloud Run service's `ingress` to `INGRESS_TRAFFIC_INTERNAL_LOAD_BALANCER`, or it stays reachable around the load balancer. - Redirecting HTTP to HTTPS needs a second URL map, target HTTP proxy and forwarding rule on port 80. ```hcl variable "project_id" { type = string description = "Project to deploy into." } variable "region" { type = string description = "Region for regional resources." } variable "domain" { type = string description = "Domain name the load balancer serves." } resource "google_cloud_run_v2_service" "app" { project = var.project_id name = "booking-app" location = var.region ingress = "INGRESS_TRAFFIC_INTERNAL_LOAD_BALANCER" template { containers { image = "us-docker.pkg.dev/cloudrun/container/hello" } } } resource "google_compute_region_network_endpoint_group" "app" { project = var.project_id name = "booking-app" region = var.region network_endpoint_type = "SERVERLESS" cloud_run { service = google_cloud_run_v2_service.app.name } } resource "google_compute_security_policy" "edge" { project = var.project_id name = "booking-edge" rule { action = "deny(403)" priority = 1000 match { expr { expression = "evaluatePreconfiguredWaf('sqli-v33-stable', {'sensitivity': 1})" } } description = "Block SQL injection." } rule { action = "allow" priority = 2147483647 match { versioned_expr = "SRC_IPS_V1" config { src_ip_ranges = ["*"] } } description = "Default rule." } } resource "google_compute_backend_service" "app" { project = var.project_id name = "booking-app" protocol = "HTTPS" load_balancing_scheme = "EXTERNAL_MANAGED" security_policy = google_compute_security_policy.edge.id backend { group = google_compute_region_network_endpoint_group.app.id } # Only signed-in identities granted roles/iap.httpsResourceAccessor get through. iap { enabled = true } log_config { enable = true sample_rate = 1.0 } } resource "google_compute_url_map" "app" { project = var.project_id name = "booking-app" default_service = google_compute_backend_service.app.id } resource "google_compute_ssl_policy" "modern" { project = var.project_id name = "tls-1-2-modern" profile = "MODERN" min_tls_version = "TLS_1_2" } resource "google_compute_managed_ssl_certificate" "app" { project = var.project_id name = "booking-app" managed { domains = [var.domain] } } resource "google_compute_target_https_proxy" "app" { project = var.project_id name = "booking-app" url_map = google_compute_url_map.app.id ssl_certificates = [google_compute_managed_ssl_certificate.app.id] ssl_policy = google_compute_ssl_policy.modern.id } resource "google_compute_global_address" "app" { project = var.project_id name = "booking-app" } resource "google_compute_global_forwarding_rule" "https" { project = var.project_id name = "booking-app-https" load_balancing_scheme = "EXTERNAL_MANAGED" ip_address = google_compute_global_address.app.id port_range = "443" target = google_compute_target_https_proxy.app.id } # HTTP to HTTPS. resource "google_compute_url_map" "redirect" { project = var.project_id name = "booking-app-redirect" default_url_redirect { https_redirect = true strip_query = false } } resource "google_compute_target_http_proxy" "redirect" { project = var.project_id name = "booking-app-redirect" url_map = google_compute_url_map.redirect.id } resource "google_compute_global_forwarding_rule" "http" { project = var.project_id name = "booking-app-http" load_balancing_scheme = "EXTERNAL_MANAGED" ip_address = google_compute_global_address.app.id port_range = "80" target = google_compute_target_http_proxy.redirect.id } # Who may pass Identity-Aware Proxy. resource "google_iap_web_backend_service_iam_member" "staff" { project = var.project_id web_backend_service = google_compute_backend_service.app.name role = "roles/iap.httpsResourceAccessor" member = "domain:${var.domain}" } ```