Skip to content

Commit 81c1d57

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

File tree

10 files changed

+291
-3
lines changed

10 files changed

+291
-3
lines changed
Lines changed: 142 additions & 0 deletions
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module "lb-frontend" {
2+
source = "../../modules/frontend"
3+
project_id = "abhiwa-test-30112023"
4+
name = "global-lb-fe-1"
5+
url_map_input = module.lb-backend.backend_service_info
6+
}
7+
8+
module "lb-backend" {
9+
source = "../../modules/backend"
10+
project_id = "abhiwa-test-30112023"
11+
name = "gcs-backend-bucket"
12+
backend_bucket_name = module.gcs.name
13+
enable_cdn = true
14+
}
15+
16+
module "gcs" {
17+
source = "terraform-google-modules/cloud-storage/google//modules/simple_bucket"
18+
version = "~> 10.0"
19+
20+
project_id = "abhiwa-test-30112023"
21+
location = "us-central1"
22+
name = "bucket-abhiwa"
23+
force_destroy = true
24+
iam_members = [{ member = "allUsers", role = "roles/storage.objectViewer" }]
25+
}
26+
27+
// The image object in Cloud Storage.
28+
// Note that the path in the bucket matches the paths in the url map path rule above.
29+
resource "google_storage_bucket_object" "image" {
30+
name = "assets/gcp-logo.svg"
31+
content = file("./gcp-logo.svg")
32+
content_type = "image/svg+xml"
33+
bucket = module.gcs.name
34+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "load-balancer-ip" {
2+
value = module.lb-frontend.external_ip
3+
}

examples/external-lb-backend-bucket/variables.tf

Whitespace-only changes.

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 ? [] : []
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({
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package external_lb_backend_bucket
16+
17+
import (
18+
"testing"
19+
20+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
21+
"github.com/stretchr/testify/assert"
22+
23+
test "github.com/terraform-google-modules/terraform-google-lb-http/test/integration"
24+
)
25+
26+
func TestSeparateFrontendAndBackend(t *testing.T) {
27+
bpt := tft.NewTFBlueprintTest(t)
28+
29+
bpt.DefineVerify(func(assert *assert.Assertions) {
30+
bpt.DefaultVerify(assert)
31+
32+
loadBalancerIp := bpt.GetStringOutput("load-balancer-ip")
33+
34+
test.AssertResponseStatus(t, assert, "http://"+loadBalancerIp+"/assets/gcp-logo.svg", 200)
35+
})
36+
37+
bpt.Test()
38+
}

0 commit comments

Comments
 (0)