FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
load_balancer.tf 2.55 KiB
Newer Older
Wajdi Hajji's avatar
Wajdi Hajji committed
# load_balancer.tf configures Cloud Load Balancer resources for the Cloud Run
# service if var.ingress_style == "load-balancer".

# A network endpoint group for the "webapp" application.
resource "google_compute_region_network_endpoint_group" "webapp" {
  count = var.ingress_style == "load-balancer" ? 1 : 0

  name                  = var.name
  network_endpoint_type = "SERVERLESS"
  region                = var.cloud_run_region
  cloud_run {
    service = google_cloud_run_service.webapp.name
  }

  provider = google-beta
}

resource "google_compute_ssl_policy" "default" {
  count = var.ingress_style == "load-balancer" && var.ssl_policy == null ? 1 : 0

  name            = "${var.name}-modern"
  profile         = "MODERN"
  min_tls_version = "TLS_1_2"
}

Wajdi Hajji's avatar
Wajdi Hajji committed
# A load balancer for the "webapp" application. This is just a set of sane
# defaults. See the full documentation at [1] for customisation.
#
# [1] https://registry.terraform.io/modules/GoogleCloudPlatform/lb-http/google/latest/submodules/serverless_negs
module "webapp_http_load_balancer" {
  count = var.ingress_style == "load-balancer" ? 1 : 0
Wajdi Hajji's avatar
Wajdi Hajji committed

  # The double slash is important(!)
  source  = "GoogleCloudPlatform/lb-http/google//modules/serverless_negs"
Wajdi Hajji's avatar
Wajdi Hajji committed

  project = var.project
  name    = var.name

  ssl            = true
  https_redirect = true

  # Use custom TLS certs if var.use_ssl_certificates is true, otherwise, use the Google-managed certs.
  use_ssl_certificates            = var.use_ssl_certificates
  ssl_certificates                = var.ssl_certificates
  managed_ssl_certificate_domains = local.dns_names
  ssl_policy                      = var.ssl_policy == null ? google_compute_ssl_policy.default[0].id : var.ssl_policy
Wajdi Hajji's avatar
Wajdi Hajji committed

  # Whether to create an IPv6 address to the load balancer.
  enable_ipv6         = var.enable_ipv6
  create_ipv6_address = var.create_ipv6_address

  backends = {
    default = {
      description             = null
      enable_cdn              = false
      custom_request_headers  = null
      custom_response_headers = null
      security_policy         = null
Wajdi Hajji's avatar
Wajdi Hajji committed

      log_config = {
        enable      = true
        sample_rate = 1.0
      }

      groups = [
        {
          group = google_compute_region_network_endpoint_group.webapp[0].id
Wajdi Hajji's avatar
Wajdi Hajji committed
        }
      ]

      # Currently Cloud IAP is not supported for Cloud Run endpoints. We still
      # need to specify that we don't want to use it though :).
      iap_config = {
        enable               = false
        oauth2_client_id     = null
        oauth2_client_secret = null
      }
    }
  }
}