Skip to content

Commit 361b4b8

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

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

modules/backend/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This module creates `google_compute_backend_service` resource and its dependenci
77
| Name | Description | Type | Default | Required |
88
|------|-------------|------|---------|:--------:|
99
| affinity\_cookie\_ttl\_sec | Lifetime of cookies in seconds if session\_affinity is GENERATED\_COOKIE. | `number` | `null` | no |
10+
| backend\_bucket\_name | The name of GCS bucket which serves the traffic. | `string` | `""` | no |
1011
| cdn\_policy | Cloud CDN configuration for this BackendService. | <pre>object({<br> cache_mode = optional(string)<br> signed_url_cache_max_age_sec = optional(string)<br> default_ttl = optional(number)<br> max_ttl = optional(number)<br> client_ttl = optional(number)<br> negative_caching = optional(bool)<br> serve_while_stale = optional(number)<br> bypass_cache_on_request_headers = optional(list(string))<br> negative_caching_policy = optional(object({<br> code = optional(number)<br> ttl = optional(number)<br> }))<br> cache_key_policy = optional(object({<br> include_host = optional(bool)<br> include_protocol = optional(bool)<br> include_query_string = optional(bool)<br> query_string_blacklist = optional(list(string))<br> query_string_whitelist = optional(list(string))<br> include_http_headers = optional(list(string))<br> include_named_cookies = optional(list(string))<br> }))<br> })</pre> | `{}` | no |
1112
| compression\_mode | Compress text responses using Brotli or gzip compression. | `string` | `"DISABLED"` | no |
1213
| connection\_draining\_timeout\_sec | Time for which instance will be drained (not accept new connections, but still work to finish started). | `number` | `null` | no |

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/metadata.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ spec:
176176
version: ">= 0.13"
177177
spec:
178178
outputExpr: "{\"region\": location, \"service_name\": service_name, \"type\": \"cloud-run\", \"service_version\": \"\"}"
179+
- name: backend_bucket_name
180+
description: The name of GCS bucket which serves the traffic.
181+
varType: string
182+
defaultValue: ""
179183
- name: iap_config
180184
description: Settings for enabling Cloud Identity Aware Proxy Structure.
181185
varType: |-

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)