Skip to content

Commit c26b8fd

Browse files
committed
feat: added example for internal cross regional lb
1 parent 8d0a402 commit c26b8fd

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
provider "google" {
18+
project = var.project_id
19+
}
20+
21+
provider "google-beta" {
22+
project = var.project_id
23+
}
24+
25+
resource "google_compute_network" "internal_lb_network" {
26+
name = "int-lb-network"
27+
auto_create_subnetworks = "false"
28+
project = var.project_id
29+
}
30+
31+
resource "google_compute_subnetwork" "internal_lb_subnet_a" {
32+
name = "int-lb-subnet-a"
33+
ip_cidr_range = "10.1.2.0/24"
34+
network = google_compute_network.internal_lb_network.id
35+
region = var.subnet_region_a
36+
project = var.project_id
37+
depends_on = [google_compute_network.internal_lb_network]
38+
}
39+
40+
resource "google_compute_subnetwork" "internal_lb_proxy_only_a" {
41+
name = "int-lb-proxy-only-subnet-a"
42+
ip_cidr_range = "10.129.0.0/23"
43+
network = google_compute_network.internal_lb_network.id
44+
purpose = "GLOBAL_MANAGED_PROXY"
45+
region = var.subnet_region_a
46+
project = var.project_id
47+
role = "ACTIVE"
48+
depends_on = [google_compute_network.internal_lb_network]
49+
}
50+
51+
resource "google_compute_subnetwork" "internal_lb_subnet_b" {
52+
name = "int-lb-subnet-b"
53+
ip_cidr_range = "10.1.3.0/24"
54+
network = google_compute_network.internal_lb_network.id
55+
region = var.subnet_region_b
56+
project = var.project_id
57+
depends_on = [google_compute_network.internal_lb_network]
58+
}
59+
60+
resource "google_compute_subnetwork" "internal_lb_proxy_only_b" {
61+
name = "int-lb-proxy-only-subnet-b"
62+
ip_cidr_range = "10.130.0.0/23"
63+
network = google_compute_network.internal_lb_network.id
64+
purpose = "GLOBAL_MANAGED_PROXY"
65+
region = var.subnet_region_b
66+
project = var.project_id
67+
role = "ACTIVE"
68+
depends_on = [google_compute_network.internal_lb_network]
69+
}
70+
71+
module "backend-service-region-a" {
72+
source = "GoogleCloudPlatform/cloud-run/google//modules/v2"
73+
version = "~> 0.16.3"
74+
project_id = var.project_id
75+
location = var.backend_region_a
76+
service_name = "bs-a"
77+
containers = [{ "container_name" = "", "container_image" = "gcr.io/cloudrun/hello" }]
78+
members = ["allUsers"]
79+
ingress = "INGRESS_TRAFFIC_INTERNAL_ONLY"
80+
cloud_run_deletion_protection = false
81+
enable_prometheus_sidecar = false
82+
}
83+
84+
module "backend-service-region-b" {
85+
source = "GoogleCloudPlatform/cloud-run/google//modules/v2"
86+
version = "~> 0.16.3"
87+
project_id = var.project_id
88+
location = var.backend_region_b
89+
service_name = "bs-b"
90+
containers = [{ "container_name" = "", "container_image" = "gcr.io/cloudrun/hello" }]
91+
members = ["allUsers"]
92+
ingress = "INGRESS_TRAFFIC_INTERNAL_ONLY"
93+
cloud_run_deletion_protection = false
94+
enable_prometheus_sidecar = false
95+
}
96+
97+
module "internal-lb-http-backend" {
98+
source = "../../modules/backend" # use registry
99+
#version = "~> 12.1.1"
100+
101+
project_id = var.project_id
102+
name = "int-lb-http-backend"
103+
enable_cdn = false
104+
load_balancing_scheme = "INTERNAL_MANAGED"
105+
locality_lb_policy = "RANDOM"
106+
compression_mode = "DISABLED"
107+
serverless_neg_backends = [
108+
{ region : var.backend_region_a, type : "cloud-run", service_name : module.backend-service-region-a.service_name },
109+
{ region : var.backend_region_b, type : "cloud-run", service_name : module.backend-service-region-b.service_name }
110+
]
111+
}
112+
113+
module "internal-lb-http-frontend" {
114+
source = "../../modules/frontend" # use registry
115+
#version = "~> 12.1.1"
116+
117+
project_id = var.project_id
118+
name = "int-lb-http-frontend"
119+
url_map_input = module.internal-lb-http-backend.backend_service_info
120+
network = google_compute_network.internal_lb_network.name
121+
load_balancing_scheme = "INTERNAL_MANAGED"
122+
internal_forwarding_rule_subnetworks = [
123+
google_compute_subnetwork.internal_lb_subnet_a.id,
124+
google_compute_subnetwork.internal_lb_subnet_b.id
125+
]
126+
#depends_on = [google_compute_subnetwork.internal_lb_proxy_only_a, google_compute_subnetwork.internal_lb_proxy_only_b, google_compute_subnetwork.internal_lb_subnet_a, google_compute_network.internal_lb_subnet_b]
127+
}
128+
129+
resource "google_vpc_access_connector" "internal_lb_vpc_connector" {
130+
provider = google-beta
131+
project = var.project_id
132+
name = "int-lb-vpc-connector"
133+
region = var.subnet_region_a
134+
ip_cidr_range = "10.8.0.0/28"
135+
network = google_compute_network.internal_lb_network.name
136+
max_throughput = 500
137+
min_throughput = 300
138+
}
139+
140+
module "frontend-service-a" {
141+
source = "GoogleCloudPlatform/cloud-run/google//modules/v2"
142+
version = "~> 0.16.3"
143+
project_id = var.project_id
144+
location = var.subnet_region_a
145+
service_name = "fs-a"
146+
containers = [{ "env_vars" : { "TARGET_IP" : module.internal-lb-http-frontend.ip_address_http_internal[0] }, "ports" = { "container_port" = 80, "name" = "http1" }, "container_name" = "", "container_image" = "gcr.io/design-center-container-repo/redirect-traffic:latest-2002" }]
147+
members = ["allUsers"]
148+
vpc_access = {
149+
connector = google_vpc_access_connector.internal_lb_vpc_connector.id
150+
egress = "ALL_TRAFFIC"
151+
}
152+
ingress = "INGRESS_TRAFFIC_ALL"
153+
cloud_run_deletion_protection = false
154+
enable_prometheus_sidecar = false
155+
depends_on = [google_vpc_access_connector.internal_lb_vpc_connector]
156+
}
157+
158+
module "frontend-service-b" {
159+
source = "GoogleCloudPlatform/cloud-run/google//modules/v2"
160+
version = "~> 0.16.3"
161+
project_id = var.project_id
162+
location = var.subnet_region_a
163+
service_name = "fs-b"
164+
containers = [{ "env_vars" : { "TARGET_IP" : module.internal-lb-http-frontend.ip_address_http_internal[1] }, "ports" = { "container_port" = 80, "name" = "http1" }, "container_name" = "", "container_image" = "gcr.io/design-center-container-repo/redirect-traffic:latest-2002" }]
165+
members = ["allUsers"]
166+
vpc_access = {
167+
connector = google_vpc_access_connector.internal_lb_vpc_connector.id
168+
egress = "ALL_TRAFFIC"
169+
}
170+
ingress = "INGRESS_TRAFFIC_ALL"
171+
cloud_run_deletion_protection = false
172+
enable_prometheus_sidecar = false
173+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
20+
21+
variable "backend_region_a" {
22+
type = string
23+
}
24+
25+
variable "backend_region_b" {
26+
type = string
27+
}
28+
29+
variable "subnet_region_a" {
30+
type = string
31+
}
32+
33+
variable "subnet_region_b" {
34+
type = string
35+
}
36+
37+
38+

0 commit comments

Comments
 (0)