SebastianG
SebastianG

Reputation: 9594

terraform gcp lb module throwing "invalid value for field 'resource.pathMatchers[0].defaultService' with basic setup, identical to example in repo

I'm trying to create a https load balancer to serve requests to cloud run services using this module: https://github.com/terraform-google-modules/terraform-google-lb-http/blob/master/examples/cloudrun/main.tf and been basing it off of the example there.

Unfortunately it throws:

Error creating UrlMap: googleapi: Error 400: Invalid value for field 'resource.pathMatchers[0].defaultService': 'https://compute.googleapis.com/compute/v1/projects/myproject-dev/regions/europe-west2/networkEndpointGroups/gateway-neg'. Unexpected resource collection 'networkEndpointGroups'.
│ More details:
│ Reason: invalid, Message: Invalid value for field 'resource.pathMatchers[0].defaultService': 'https://compute.googleapis.com/compute/v1/projects/myproject-dev/regions/europe-west2/networkEndpointGroups/gateway-neg'. Unexpected resource collection 'networkEndpointGroups'.
│ Reason: invalid, Message: Invalid value for field 'resource.defaultService': 'https://compute.googleapis.com/compute/v1/projects/myproject-dev/regions/europe-west2/networkEndpointGroups/gateway-neg'. Unexpected resource collection 'networkEndpointGroups'.

Full load balancer code:


resource "google_compute_global_address" "main" {
  name = "main"
}

module "lb" {
  source  = "GoogleCloudPlatform/lb-http/google//modules/serverless_negs"
  version = "~> 6.3.0"
  project = var.gcp_project
  name    = "main"
  address = google_compute_global_address.main.address


  http_forward                    = true
  https_redirect                  = true
  managed_ssl_certificate_domains = [join("", [var.environment, ".yeo.center"])]
  create_address                  = false
  url_map                         = google_compute_url_map.main.self_link
  create_url_map                  = false
  ssl                             = true

  backends = {
    default = {
      description = null
      groups = [
        {
          group = google_compute_region_network_endpoint_group.gateway-neg.id
        }
      ]
      enable_cdn              = false
      security_policy         = null
      custom_request_headers  = null
      custom_response_headers = null

      iap_config = {
        enable               = false
        oauth2_client_id     = ""
        oauth2_client_secret = ""
      }
      log_config = {
        enable      = true
        sample_rate = null
      }
    }
  }

  depends_on = [
    google_compute_region_network_endpoint_group.gateway-neg,
  ]

}


resource "google_compute_url_map" "main" {
  name            = "main"
  default_service = google_compute_region_network_endpoint_group.gateway-neg.id

  host_rule {
    hosts        = ["*"]
    path_matcher = "allpaths"
  }

  path_matcher {
    name            = "allpaths"
    default_service = google_compute_region_network_endpoint_group.gateway-neg.id

    path_rule {
      paths = [
        "/apis",
        "/apis/gateway/*"
      ]
      service = module.lb.backend_services["default"].id
    }
  }

  depends_on = [
    google_compute_region_network_endpoint_group.gateway-neg,
  ]

}


resource "google_compute_region_network_endpoint_group" "gateway-neg" {
  name                  = "gateway-neg"
  network_endpoint_type = "SERVERLESS"
  region                = var.gcp_region
  cloud_run {
    service = google_cloud_run_service.gateway.name
  }
}

Any ideas on the right configuration here?

Upvotes: 0

Views: 1802

Answers (1)

SND
SND

Reputation: 1557

Bit late to the party, but I was trying the same thing and stumbled on the exact same error. To resolve the issue, the default_service should not be linked to the network endpoint group, but rather to the backend service output by the load balancer module.

For OP's example, this would translate to module.lb.backend_services.default.id.

Furthermore, note that enabling advanced routing rules is not supported for classic EXTERNAL load balancing schemes, but is supported for the EXTERNAL_MANAGED scheme. To do this, add load_balancing_scheme = "EXTERNAL_MANAGED" to the load balancer module.

After making these two changes, the Terraform script ran successfully.

Upvotes: 0

Related Questions