|
| 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" "default" { |
| 26 | + name = "lb-internal-cloud-run" |
| 27 | + auto_create_subnetworks = "false" |
| 28 | + project = var.project_id |
| 29 | +} |
| 30 | + |
| 31 | +resource "google_compute_subnetwork" "subnet_a" { |
| 32 | + name = "subnet-a" |
| 33 | + ip_cidr_range = "10.1.2.0/24" |
| 34 | + network = google_compute_network.default.id |
| 35 | + region = var.subnet_region_a |
| 36 | + project = var.project_id |
| 37 | + depends_on = [google_compute_network.default] |
| 38 | +} |
| 39 | + |
| 40 | +resource "google_compute_subnetwork" "proxy_only_a" { |
| 41 | + name = "proxy-only-subnet-a" |
| 42 | + ip_cidr_range = "10.129.0.0/23" |
| 43 | + network = google_compute_network.default.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.default] |
| 49 | +} |
| 50 | + |
| 51 | +resource "google_compute_subnetwork" "subnet_b" { |
| 52 | + name = "subnet-b" |
| 53 | + ip_cidr_range = "10.1.3.0/24" |
| 54 | + network = google_compute_network.default.id |
| 55 | + region = var.subnet_region_b |
| 56 | + project = var.project_id |
| 57 | + depends_on = [google_compute_network.default] |
| 58 | +} |
| 59 | + |
| 60 | +resource "google_compute_subnetwork" "proxy_only_b" { |
| 61 | + name = "proxy-only-subnet-b" |
| 62 | + ip_cidr_range = "10.130.0.0/23" |
| 63 | + network = google_compute_network.default.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.default] |
| 69 | +} |
| 70 | + |
| 71 | +module "backend-service-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-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 "lb-http-backend" { |
| 98 | + source = "../../modules/backend" # use registry |
| 99 | + #version = "~> 12.1.1" |
| 100 | + |
| 101 | + project_id = var.project_id |
| 102 | + name = "backend-lb-int" |
| 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-a.service_name }, |
| 109 | + { region : var.backend_region_b, type : "cloud-run", service_name : module.backend-service-b.service_name } |
| 110 | + ] |
| 111 | +} |
| 112 | + |
| 113 | +module "lb-http-frontend" { |
| 114 | + source = "../../modules/frontend" # use registry |
| 115 | + #version = "~> 12.1.1" |
| 116 | + |
| 117 | + project_id = var.project_id |
| 118 | + name = "frontend-lb-int" |
| 119 | + url_map_input = module.lb-http-backend.backend_service_info |
| 120 | + network = google_compute_network.default.name |
| 121 | + load_balancing_scheme = "INTERNAL_MANAGED" |
| 122 | + internal_forwarding_rule_subnetworks = [ |
| 123 | + google_compute_subnetwork.subnet_a.id, |
| 124 | + google_compute_subnetwork.subnet_b.id |
| 125 | + ] |
| 126 | + #depends_on = [google_compute_subnetwork.proxy_only_a, google_compute_subnetwork.proxy_only_b] |
| 127 | +} |
| 128 | + |
| 129 | +resource "google_vpc_access_connector" "default" { |
| 130 | + provider = google-beta |
| 131 | + project = var.project_id |
| 132 | + name = "fe-vpc-cx" |
| 133 | + region = var.subnet_region_a |
| 134 | + ip_cidr_range = "10.8.0.0/28" |
| 135 | + network = google_compute_network.default.name |
| 136 | + max_throughput = 500 |
| 137 | + min_throughput = 300 |
| 138 | +} |
| 139 | + |
| 140 | +module "frontend-service" { |
| 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-2002" |
| 146 | + containers = [{ "env_vars" : { "TARGET_IP" : module.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.default.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.default] |
| 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-2002" |
| 164 | + containers = [{ "env_vars" : { "TARGET_IP" : module.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.default.id |
| 168 | + egress = "ALL_TRAFFIC" |
| 169 | + } |
| 170 | + ingress = "INGRESS_TRAFFIC_ALL" |
| 171 | + cloud_run_deletion_protection = false |
| 172 | + enable_prometheus_sidecar = false |
| 173 | +} |
0 commit comments