我正在设置一个带有存储桶的 ELB,以通过 Terraform 为 GCP 上的静态 HTTPS 网站提供服务。 TF 计划运行良好,但我的创建无法设置 URL 映射。
TF云错误:
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"
当我卷曲引用的 URL (
https://www.googleapis.com/storage/v1/b/eef9da33ed80dd35-static-website-bucket
) 时,我收到以下响应:
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" {
// 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
}
}
}
在查看源代码存储库后,我发现我的代码中缺少一个资源块。 path_rule需要指向不同的TF资源:
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
}
此资源引用上面创建的存储桶,然后用于提供
self_link
属性来显示 url。