Skip to content

Commit 3e3e636

Browse files
committed
feat: add support for backend bucket to modules/backend
1 parent 6d6664b commit 3e3e636

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

modules/backend/main.tf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17+
locals {
18+
is_backend_bucket = var.backend_bucket_name != null && var.backend_bucket_name != ""
19+
}
20+
1721
resource "google_compute_backend_service" "default" {
1822
provider = google-beta
23+
count = !local.is_backend_bucket ? 1 : 0
1924

2025
project = var.project_id
2126
name = var.name
@@ -310,3 +315,52 @@ resource "google_compute_firewall" "allow_proxy" {
310315
protocol = "tcp"
311316
}
312317
}
318+
319+
resource "google_compute_backend_bucket" "default" {
320+
provider = google-beta
321+
count = local.is_backend_bucket ? 1 : 0
322+
323+
project = var.project_id
324+
name = var.name
325+
bucket_name = var.backend_bucket_name
326+
enable_cdn = var.enable_cdn
327+
328+
description = var.description
329+
330+
# CDN policy configuration, if CDN is enabled
331+
dynamic "cdn_policy" {
332+
for_each = var.enable_cdn ? [1] : []
333+
content {
334+
cache_mode = var.cdn_policy.cache_mode
335+
signed_url_cache_max_age_sec = var.cdn_policy.signed_url_cache_max_age_sec
336+
default_ttl = var.cdn_policy.default_ttl
337+
max_ttl = var.cdn_policy.max_ttl
338+
client_ttl = var.cdn_policy.client_ttl
339+
negative_caching = var.cdn_policy.negative_caching
340+
serve_while_stale = var.cdn_policy.serve_while_stale
341+
342+
dynamic "negative_caching_policy" {
343+
for_each = var.cdn_policy.negative_caching_policy != null ? [1] : []
344+
content {
345+
code = var.cdn_policy.negative_caching_policy.code
346+
ttl = var.cdn_policy.negative_caching_policy.ttl
347+
}
348+
}
349+
350+
dynamic "cache_key_policy" {
351+
for_each = var.cdn_policy.cache_key_policy != null ? [1] : []
352+
content {
353+
query_string_whitelist = var.cdn_policy.cache_key_policy.query_string_whitelist
354+
include_http_headers = var.cdn_policy.cache_key_policy.include_http_headers
355+
}
356+
}
357+
358+
dynamic "bypass_cache_on_request_headers" {
359+
for_each = var.cdn_policy.bypass_cache_on_request_headers != null && length(var.cdn_policy.bypass_cache_on_request_headers) > 0 ? toset(var.cdn_policy.bypass_cache_on_request_headers) : []
360+
content {
361+
header_name = bypass_cache_on_request_headers.value
362+
}
363+
}
364+
}
365+
}
366+
}

modules/backend/outputs.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616

1717
output "backend_service_info" {
1818
description = "Host, path and backend service mapping"
19-
value = [
19+
value = concat(!local.is_backend_bucket ? [
2020
for mapping in var.host_path_mappings : {
2121
host = mapping.host
2222
path = mapping.path
23-
backend_service = google_compute_backend_service.default.self_link
23+
backend_service = google_compute_backend_service.default[0].self_link
2424
}
25-
]
25+
] : [], local.is_backend_bucket ? [for mapping in var.host_path_mappings : {
26+
host = mapping.host
27+
path = mapping.path
28+
backend_service = google_compute_backend_bucket.default[0].self_link
29+
}
30+
] : []
31+
)
2632
}

modules/backend/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ variable "serverless_neg_backends" {
147147
}
148148
}
149149

150+
variable "backend_bucket_name" {
151+
description = "The name of GCS bucket which serves the traffic."
152+
type = string
153+
default = ""
154+
}
155+
150156
variable "iap_config" {
151157
description = "Settings for enabling Cloud Identity Aware Proxy Structure."
152158
type = object({

0 commit comments

Comments
 (0)