How to configure Path base routing to different backen in gcp?

161 Views Asked by At

I have two cloud run service which works as backends for internal load balancer. I want to route the traffic two different backend service based on the path. Following is the terraform code . I want to route all the traffic other then /api/id to first-cloud-run service and only forward the /api/id traffic to second-cloud-run backend (also for /api/id I want to rewrite the url as the backend only response to /id path)

# URL map
resource "google_compute_region_url_map" "url_map_euw3" {
  name    = "${var.lb_prefix}-ilb-regional-url-map"
  project = frontend_project.project_id
  region  = "europe-west1"

  default_service = google_compute_region_backend_service.service_euw3["first-cloud-run"].id

  host_rule {
    hosts        = ["*"]
    path_matcher = "default-path-matcher"
  }

   path_matcher {
    name            = "default-path-matcher"
    default_service = google_compute_region_backend_service.service_euw3["first-cloud-run"].id
    path_rule {
      paths   = ["/api/id"]
      service = google_compute_region_backend_service.service_euw3["second-cloud-run"].id
      route_action {
        url_rewrite {
          path_prefix_rewrite = "/id"
        }
      } 
    }
    path_rule {
      paths   = ["/*"]
      service = google_compute_region_backend_service.service_euw3["first-cloud-run"].id
    }
  }

}
0

There are 0 best solutions below