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