Terraform x GCP – URL map has invalid URL Terraform x GCP – URL映射具有无效的URL

huangapple go评论111阅读模式
英文:

Terraform x GCP - URL map has invalid URL

问题

TF计划运行正常,但我的创建操作无法设置URL映射。TF Cloud错误信息如下:

错误: 创建UrlMap时出错: googleapi: 字段 'resource.pathMatchers[0].pathRules[0].service' 的值无效: 'https://www.googleapis.com/storage/v1/b/eef9da33ed80dd35-static-website-bucket'。URL格式错误。, 在main.tf的第81行,resource "google_compute_url_map" "my-https-network"中出现问题。

当我使用引用的URL(https://www.googleapis.com/storage/v1/b/eef9da33ed80dd35-static-website-bucket)进行curl请求时,我收到以下响应:

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

我的Terraform设置如下:

  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"
    }
  }
}

负载均衡器源模块为"GoogleCloudPlatform/lb-http/google",可在https://github.com/terraform-google-modules/terraform-google-lb 找到。

我相关的TF模块/资源如下:

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" {
  # 这是负载均衡器的名称
  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
    }
  }
}
英文:

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 &#39;resource.pathMatchers[0].pathRules[0].service&#39;: &#39;https://www.googleapis.com/storage/v1/b/eef9da33ed80dd35-static-website-bucket&#39;. The URL is malformed., invalid with google_compute_url_map.my-https-network on main.tf line 81, in resource &quot;google_compute_url_map&quot; &quot;my-https-network&quot;

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=&quot;:443&quot;; ma=2592000,h3-29=&quot;:443&quot;; ma=2592000

My Terraform setup is:

  required_version = &quot;&gt;= 1.1.2&quot;
  required_providers {
    google = {
      source  = &quot;hashicorp/google&quot;
      version = &quot;&gt;= 3.53, &lt; 5.0&quot;
    }
    google-beta = {
      source  = &quot;hashicorp/google-beta&quot;
      version = &quot;&gt;= 4.40, &lt; 5.0&quot;
    }
    random = {
      source = &quot;hashicorp/random&quot;
    }
    tls = {
      source = &quot;hashicorp/tls&quot;
    }
  }

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

My relevant TF modules/resources are:

resource &quot;google_storage_bucket&quot; &quot;static_website&quot; {
name          = &quot;${random_id.bucket_prefix.hex}-static-website-bucket&quot;
location      = &quot;US&quot;
storage_class = &quot;STANDARD&quot;
website {
main_page_suffix = &quot;index.html&quot;
not_found_page   = &quot;404.html&quot;
}
}
module &quot;gce-lb-https&quot; {
source  = &quot;GoogleCloudPlatform/lb-http/google&quot;
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    = &quot;HTTP&quot;
port        = 80
port_name   = &quot;http&quot;
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 &quot;google_compute_url_map&quot; &quot;my-https-network&quot; {
// note that this is the name of the load balancer
name            = var.network_name
default_service = module.gce-lb-https.backend_services[&quot;default&quot;].self_link
host_rule {
hosts        = [&quot;*&quot;]
path_matcher = &quot;allpaths&quot;
}
path_matcher {
name            = &quot;allpaths&quot;
default_service = module.gce-lb-https.backend_services[&quot;default&quot;].self_link
path_rule {
paths = [
&quot;/&quot;,
&quot;/*&quot;
]
service = google_storage_bucket.static_website.self_link
}
}
}
</details>
# 答案1
**得分**: 1
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:
```plaintext
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.

英文:

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 &quot;google_compute_backend_bucket&quot; &quot;static_website&quot; {
name        = random_id.bucket_prefix.hex
description = &quot;Contains static resources for the app&quot;
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.

huangapple
  • 本文由 发表于 2023年8月5日 01:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定