three-tier-gke/infra/network.tf (78 lines of code) (raw):

/** Copyright 2023 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ module "vpc" { source = "terraform-google-modules/network/google" version = "~> 5.1.0" project_id = var.project_id network_name = "${var.application_name}-vpc" routing_mode = "GLOBAL" subnets = [ { subnet_name = "subnet-${var.region}" subnet_ip = local.cluster_ip_ranges.nodes subnet_region = var.region subnet_private_access = true }, { subnet_name = "proxy-only-subnet" subnet_ip = "10.129.0.0/23" subnet_region = var.region purpose = "REGIONAL_MANAGED_PROXY" role = "ACTIVE" }, ] secondary_ranges = { "subnet-${var.region}" = [ { range_name = "pods" ip_cidr_range = local.cluster_ip_ranges.pods }, { range_name = "services" ip_cidr_range = local.cluster_ip_ranges.services }, ] } } resource "google_compute_router" "nat_router" { name = "${module.vpc.network_name}-nat-router" network = module.vpc.network_self_link region = var.region bgp { asn = 64514 } } resource "google_compute_router_nat" "nat_gateway" { name = "${module.vpc.network_name}-nat-gw" router = google_compute_router.nat_router.name region = google_compute_router.nat_router.region nat_ip_allocate_option = "AUTO_ONLY" source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES" log_config { enable = true filter = "ERRORS_ONLY" } } resource "google_compute_global_address" "service_range" { name = "service-networking-address" purpose = "VPC_PEERING" address_type = "INTERNAL" address = "10.200.0.0" prefix_length = 16 network = module.vpc.network_name } resource "google_service_networking_connection" "service_networking" { network = module.vpc.network_id service = "servicenetworking.googleapis.com" reserved_peering_ranges = [google_compute_global_address.service_range.name] }