英文:
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 '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
}
}
}
</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 "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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论