Skip to content

Commit

Permalink
feat: Make backend services optional in lb modules
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewscwei committed Oct 18, 2024
1 parent 46a5659 commit 1db6baa
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
8 changes: 4 additions & 4 deletions http_lb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ resource "google_compute_url_map" "redirect" {
# Create a basic URL map for the load balancer if `create_url_map` is `true`.
# This URL map routes all paths to the first Backend Service resource created.
resource "google_compute_url_map" "default" {
default_service = module.backend_service[0].self_link
default_service = length(module.backend_service) > 0 ? module.backend_service[0].self_link : null
name = "${var.name}-url-map"
project = var.project

Expand All @@ -175,10 +175,10 @@ resource "google_compute_url_map" "default" {

content {
name = "path-matcher${path_matcher.key}"
default_service = path_matcher.value.default_url_redirect == null ? lookup(module.backend_service[path_matcher.value.default_backend_service_index], "self_link") : null
default_service = (length(module.backend_service) > 0 && path_matcher.value.default_url_redirect == null) ? lookup(module.backend_service[path_matcher.value.default_backend_service_index], "self_link") : null

dynamic "default_url_redirect" {
for_each = path_matcher.value.default_url_redirect == null ? [] : [path_matcher.value.default_url_redirect]
for_each = (length(module.backend_service) > 0 && path_matcher.value.default_url_redirect == null) ? [] : [path_matcher.value.default_url_redirect]

content {
host_redirect = default_url_redirect.value.host_redirect
Expand All @@ -192,7 +192,7 @@ resource "google_compute_url_map" "default" {

content {
paths = path_rule.value.paths
service = lookup(module.backend_service[path_rule.value.backend_service_index], "self_link")
service = length(module.backend_service) > 0 ? lookup(module.backend_service[path_rule.value.backend_service_index], "self_link") : null
}
}
}
Expand Down
1 change: 1 addition & 0 deletions http_lb/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
variable "backend_services" {
default = []
description = "Configuration for each `backend_service` module."
type = list(object({
acl = optional(string, "publicread")
Expand Down
6 changes: 3 additions & 3 deletions http_lb_regional/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ resource "google_compute_url_map" "redirect" {
# Create a basic URL map for the load balancer. This URL map routes all paths to
# the first Backend Service resource created.
resource "google_compute_url_map" "default" {
default_service = module.backend_service[0].self_link
default_service = length(module.backend_service) > 0 ? module.backend_service[0].self_link : null
name = "${var.name}-url-map"
project = var.project

Expand All @@ -181,7 +181,7 @@ resource "google_compute_url_map" "default" {

content {
name = "path-matcher${path_matcher.key}"
default_service = path_matcher.value.default_url_redirect == null ? lookup(module.backend_service[path_matcher.value.default_backend_service_index], "self_link") : null
default_service = (length(module.backend_service) > 0 && path_matcher.value.default_url_redirect == null) ? lookup(module.backend_service[path_matcher.value.default_backend_service_index], "self_link") : null

dynamic "default_url_redirect" {
for_each = path_matcher.value.default_url_redirect == null ? [] : [path_matcher.value.default_url_redirect]
Expand All @@ -198,7 +198,7 @@ resource "google_compute_url_map" "default" {

content {
paths = path_rule.value.paths
service = lookup(module.backend_service[path_rule.value.backend_service_index], "self_link")
service = length(module.backend_service) > 0 ? lookup(module.backend_service[path_rule.value.backend_service_index], "self_link") : null
}
}
}
Expand Down
1 change: 1 addition & 0 deletions http_lb_regional/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
variable "backend_services" {
default = []
description = "Configuration for each `backend_service` module."
type = list(object({
acl = optional(string, "publicread")
Expand Down

0 comments on commit 1db6baa

Please sign in to comment.