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

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

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请求时,我收到以下响应:

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

我的Terraform设置如下:

  1. required_version = ">= 1.1.2"
  2. required_providers {
  3. google = {
  4. source = "hashicorp/google"
  5. version = ">= 3.53, < 5.0"
  6. }
  7. google-beta = {
  8. source = "hashicorp/google-beta"
  9. version = ">= 4.40, < 5.0"
  10. }
  11. random = {
  12. source = "hashicorp/random"
  13. }
  14. tls = {
  15. source = "hashicorp/tls"
  16. }
  17. }
  18. }

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

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

  1. resource "google_storage_bucket" "static_website" {
  2. name = "${random_id.bucket_prefix.hex}-static-website-bucket"
  3. location = "US"
  4. storage_class = "STANDARD"
  5. website {
  6. main_page_suffix = "index.html"
  7. not_found_page = "404.html"
  8. }
  9. }
  10. module "gce-lb-https" {
  11. source = "GoogleCloudPlatform/lb-http/google"
  12. name = var.network_name
  13. project = var.project_id
  14. target_tags = []
  15. firewall_networks = [google_compute_network.default.self_link]
  16. url_map = google_compute_url_map.my-network.self_link
  17. create_url_map = false
  18. ssl = true
  19. private_key = tls_private_key.my-app.private_key_pem
  20. certificate = tls_self_signed_cert.my-app.cert_pem
  21. backends = {
  22. default = {
  23. protocol = "HTTP"
  24. port = 80
  25. port_name = "http"
  26. timeout_sec = 10
  27. enable_cdn = false
  28. groups = []
  29. health_check = local.health_check
  30. log_config = {
  31. enable = true
  32. sample_rate = 1.0
  33. }
  34. iap_config = {
  35. enable = false
  36. }
  37. }
  38. }
  39. }
  40. resource "google_compute_url_map" "my-https-network" {
  41. # 这是负载均衡器的名称
  42. name = var.network_name
  43. default_service = module.gce-lb-https.backend_services["default"].self_link
  44. host_rule {
  45. hosts = ["*"]
  46. path_matcher = "allpaths"
  47. }
  48. path_matcher {
  49. name = "allpaths"
  50. default_service = module.gce-lb-https.backend_services["default"].self_link
  51. path_rule {
  52. paths = [
  53. "/",
  54. "/*"
  55. ]
  56. service = google_storage_bucket.static_website.self_link
  57. }
  58. }
  59. }
英文:

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:

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

My Terraform setup is:

  1. required_version = &quot;&gt;= 1.1.2&quot;
  2. required_providers {
  3. google = {
  4. source = &quot;hashicorp/google&quot;
  5. version = &quot;&gt;= 3.53, &lt; 5.0&quot;
  6. }
  7. google-beta = {
  8. source = &quot;hashicorp/google-beta&quot;
  9. version = &quot;&gt;= 4.40, &lt; 5.0&quot;
  10. }
  11. random = {
  12. source = &quot;hashicorp/random&quot;
  13. }
  14. tls = {
  15. source = &quot;hashicorp/tls&quot;
  16. }
  17. }

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:

  1. resource &quot;google_storage_bucket&quot; &quot;static_website&quot; {
  2. name = &quot;${random_id.bucket_prefix.hex}-static-website-bucket&quot;
  3. location = &quot;US&quot;
  4. storage_class = &quot;STANDARD&quot;
  5. website {
  6. main_page_suffix = &quot;index.html&quot;
  7. not_found_page = &quot;404.html&quot;
  8. }
  9. }
  10. module &quot;gce-lb-https&quot; {
  11. source = &quot;GoogleCloudPlatform/lb-http/google&quot;
  12. name = var.network_name
  13. project = var.project_id
  14. target_tags = []
  15. firewall_networks = [google_compute_network.default.self_link]
  16. url_map = google_compute_url_map.my-network.self_link
  17. create_url_map = false
  18. ssl = true
  19. private_key = tls_private_key.my-app.private_key_pem
  20. certificate = tls_self_signed_cert.my-app.cert_pem
  21. backends = {
  22. default = {
  23. protocol = &quot;HTTP&quot;
  24. port = 80
  25. port_name = &quot;http&quot;
  26. timeout_sec = 10
  27. enable_cdn = false
  28. groups = []
  29. health_check = local.health_check
  30. log_config = {
  31. enable = true
  32. sample_rate = 1.0
  33. }
  34. iap_config = {
  35. enable = false
  36. }
  37. }
  38. }
  39. }
  40. resource &quot;google_compute_url_map&quot; &quot;my-https-network&quot; {
  41. // note that this is the name of the load balancer
  42. name = var.network_name
  43. default_service = module.gce-lb-https.backend_services[&quot;default&quot;].self_link
  44. host_rule {
  45. hosts = [&quot;*&quot;]
  46. path_matcher = &quot;allpaths&quot;
  47. }
  48. path_matcher {
  49. name = &quot;allpaths&quot;
  50. default_service = module.gce-lb-https.backend_services[&quot;default&quot;].self_link
  51. path_rule {
  52. paths = [
  53. &quot;/&quot;,
  54. &quot;/*&quot;
  55. ]
  56. service = google_storage_bucket.static_website.self_link
  57. }
  58. }
  59. }
  60. </details>
  61. # 答案1
  62. **得分**: 1
  63. 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:
  64. ```plaintext
  65. resource "google_compute_backend_bucket" "static_website" {
  66. name = random_id.bucket_prefix.hex
  67. description = "Contains static resources for the app"
  68. bucket_name = google_storage_bucket.static_website.name
  69. enable_cdn = true
  70. }

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:

  1. resource &quot;google_compute_backend_bucket&quot; &quot;static_website&quot; {
  2. name = random_id.bucket_prefix.hex
  3. description = &quot;Contains static resources for the app&quot;
  4. bucket_name = google_storage_bucket.static_website.name
  5. enable_cdn = true
  6. }

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:

确定