Search code examples
google-cloud-platformurlhttpsterraformterraform-provider-gcp

Terraform x GCP - URL map has invalid URL


I am setting up an ELB with a storage bucket to serve a static HTTPS site on GCP via Terraform. TF plans run fine, but my create is failing to set up the URL map.

TF Cloud Error:

Error: Error creating UrlMap: googleapi: Error 400: Invalid value for field 'resource.pathMatchers[0].pathRules[0].service': 'https://www.googleapis.com/storage/v1/b/eef9da33ed80dd35-static-website-bucket'. The URL is malformed., invalid with google_compute_url_map.my-https-network on main.tf line 81, in resource "google_compute_url_map" "my-https-network"

When I curl the quoted URL (https://www.googleapis.com/storage/v1/b/eef9da33ed80dd35-static-website-bucket), I get the following response:

curl -I https://www.googleapis.com/storage/v1/b/eef9da33ed80dd35-static-website-bucket
                
HTTP/2 200
x-guploader-uploadid: ADPycdslp8INsL__5hlmPHtkK8HUr4j1YOBpnnrpkGFqNfMmFKD82O3M4RciiHRrgqXh__wCccgJfjcR2WeQGlPM2mQ_pMMYGV2_
etag: CAI=
content-type: application/json; charset=UTF-8
date: Fri, 04 Aug 2023 17:41:13 GMT
vary: Origin
vary: X-Origin
cache-control: private, max-age=0, must-revalidate, no-transform
expires: Fri, 04 Aug 2023 17:41:13 GMT
server: UploadServer
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

My Terraform setup is:

  required_version = ">= 1.1.2"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = ">= 3.53, < 5.0"
    }
    google-beta = {
      source  = "hashicorp/google-beta"
      version = ">= 4.40, < 5.0"
    }
    random = {
      source = "hashicorp/random"
    }
    tls = {
      source = "hashicorp/tls"
    }
  }

The load balancer source module is "GoogleCloudPlatform/lb-http/google" and is found at https://github.com/terraform-google-modules/terraform-google-lb

My relevant TF modules/resources are:

resource "google_storage_bucket" "static_website" {
  name          = "${random_id.bucket_prefix.hex}-static-website-bucket"
  location      = "US"
  storage_class = "STANDARD"
  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
}

module "gce-lb-https" {
  source  = "GoogleCloudPlatform/lb-http/google"
  name    = var.network_name
  project = var.project_id
  target_tags = []
  firewall_networks = [google_compute_network.default.self_link]
  url_map           = google_compute_url_map.my-network.self_link
  create_url_map    = false
  ssl               = true
  private_key       = tls_private_key.my-app.private_key_pem
  certificate       = tls_self_signed_cert.my-app.cert_pem

  backends = {
    default = {
      protocol    = "HTTP"
      port        = 80
      port_name   = "http"
      timeout_sec = 10
      enable_cdn  = false
      groups      = []
      health_check = local.health_check
      log_config = {
        enable      = true
        sample_rate = 1.0
      }

      iap_config = {
        enable = false
      }
    }
  }
}

resource "google_compute_url_map" "my-https-network" {
  // note that this is the name of the load balancer
  name            = var.network_name
  default_service = module.gce-lb-https.backend_services["default"].self_link

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

  path_matcher {
    name            = "allpaths"
    default_service = module.gce-lb-https.backend_services["default"].self_link

    path_rule {
      paths = [
        "/",
        "/*"
      ]
      service = google_storage_bucket.static_website.self_link
    }
  }
}

Solution

  • I discovered I was missing a resource block in my code after reviewing the source repository. The path_rule needs to point to a different TF Resource:

    resource "google_compute_backend_bucket" "static_website" {
      name        = random_id.bucket_prefix.hex
      description = "Contains static resources for the app"
      bucket_name = google_storage_bucket.static_website.name
      enable_cdn  = true
    }
    

    This resource references the bucket made above, and is then used to provide the self_link attribute to surface the url.