Skip to content

Commit 1789d55

Browse files
authored
feat: add support for backend bucket to modules/backend (#511)
1 parent 10476fa commit 1789d55

File tree

14 files changed

+362
-3
lines changed

14 files changed

+362
-3
lines changed
Lines changed: 142 additions & 0 deletions
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "lb-frontend" {
18+
source = "terraform-google-modules/lb-http/google//modules/frontend"
19+
version = "~> 12.0"
20+
21+
project_id = var.project_id
22+
name = "global-lb-fe-bucket"
23+
url_map_input = module.lb-backend.backend_service_info
24+
}
25+
26+
module "lb-backend" {
27+
source = "terraform-google-modules/lb-http/google//modules/backend"
28+
version = "~> 12.0"
29+
30+
project_id = var.project_id
31+
name = "global-lb-be-bucket"
32+
backend_bucket_name = module.gcs.name
33+
enable_cdn = true
34+
}
35+
36+
module "gcs" {
37+
source = "terraform-google-modules/cloud-storage/google//modules/simple_bucket"
38+
version = "~> 10.0"
39+
40+
project_id = var.project_id
41+
location = "us-central1"
42+
name = "gcs-bucket"
43+
force_destroy = true
44+
iam_members = [{ member = "allUsers", role = "roles/storage.objectViewer" }]
45+
}
46+
47+
// The image object in Cloud Storage.
48+
// Note that the path in the bucket matches the paths in the url map path rule above.
49+
resource "google_storage_bucket_object" "image" {
50+
name = "assets/gcp-logo.svg"
51+
content = file("./gcp-logo.svg")
52+
content_type = "image/svg+xml"
53+
bucket = module.gcs.name
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "load-balancer-ip" {
18+
value = module.lb-frontend.external_ip
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
type = string
19+
}

metadata.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ spec:
5252
location: examples/cross-project-mig-backend
5353
- name: dynamic-backend
5454
location: examples/dynamic-backend
55+
- name: external-lb-backend-bucket
56+
location: examples/external-lb-backend-bucket
5557
- name: gke-node-port
5658
location: examples/https-gke/gke-node-port
5759
- name: https-gke

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 && try(length(var.cdn_policy.bypass_cache_on_request_headers), 0) > 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+
}

0 commit comments

Comments
 (0)