|
| 1 | +/** |
| 2 | + * Copyright 2024 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 | +module "network_connectivity_center" { |
| 18 | + source = "terraform-google-modules/network/google//modules/network-connectivity-center" |
| 19 | + project_id = var.project_id |
| 20 | + ncc_hub_name = var.ncc_hub_name |
| 21 | + ncc_hub_labels = { |
| 22 | + "module" = "ncc" |
| 23 | + } |
| 24 | + spoke_labels = { |
| 25 | + "created-by" = "terraform-google-ncc-example" |
| 26 | + } |
| 27 | + vpc_spokes = { |
| 28 | + "vpc-1" = { |
| 29 | + uri = module.vpc_spoke_vpc.network_id |
| 30 | + labels = { |
| 31 | + "spoke-type" = "vpc" |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + hybrid_spokes = { |
| 36 | + "vpn-1" = { |
| 37 | + type = "vpn" |
| 38 | + uris = [for k, v in module.local_to_remote_vpn.tunnel_self_links : v] |
| 39 | + site_to_site_data_transfer = true |
| 40 | + location = var.vpn_region |
| 41 | + } |
| 42 | + } |
| 43 | + router_appliance_spokes = { |
| 44 | + "appliance-1" = { |
| 45 | + instances = [ |
| 46 | + { |
| 47 | + virtual_machine = google_compute_instance.router_appliance_1.id |
| 48 | + ip_address = google_compute_instance.router_appliance_1.network_interface[0].network_ip |
| 49 | + }, |
| 50 | + |
| 51 | + ] |
| 52 | + location = var.instance_region |
| 53 | + site_to_site_data_transfer = false |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +################################ |
| 59 | +# VPC Spoke # |
| 60 | +################################ |
| 61 | +module "vpc_spoke_vpc" { |
| 62 | + source = "terraform-google-modules/network/google" |
| 63 | + project_id = var.project_id |
| 64 | + network_name = var.vpc_spoke_vpc_name |
| 65 | + routing_mode = "GLOBAL" |
| 66 | + |
| 67 | + subnets = [ |
| 68 | + { |
| 69 | + subnet_name = "vpc-spoke-subnet-01" |
| 70 | + subnet_ip = "10.10.10.0/24" |
| 71 | + subnet_region = "us-west1" |
| 72 | + }, |
| 73 | + { |
| 74 | + subnet_name = "vpc-spoke-subnet-02" |
| 75 | + subnet_ip = "10.10.20.0/24" |
| 76 | + subnet_region = "us-east1" |
| 77 | + }, |
| 78 | + { |
| 79 | + subnet_name = "vpc-spoke-subnet-03" |
| 80 | + subnet_ip = "10.10.30.0/24" |
| 81 | + subnet_region = "europe-west4" |
| 82 | + } |
| 83 | + ] |
| 84 | +} |
| 85 | + |
| 86 | +################################ |
| 87 | +# VPN Spoke # |
| 88 | +################################ |
| 89 | +# Simulates an on-prem network that will be connected over VPN |
| 90 | +module "vpn_spoke_remote_vpc" { |
| 91 | + source = "terraform-google-modules/network/google" |
| 92 | + project_id = var.project_id |
| 93 | + network_name = var.vpn_spoke_remote_vpc_name |
| 94 | + routing_mode = "GLOBAL" |
| 95 | + |
| 96 | + subnets = [ |
| 97 | + { |
| 98 | + subnet_name = "vpn-subnet-01" |
| 99 | + subnet_ip = "10.20.10.0/24" |
| 100 | + subnet_region = "us-west1" |
| 101 | + }, |
| 102 | + { |
| 103 | + subnet_name = "vpn-subnet-02" |
| 104 | + subnet_ip = "10.20.20.0/24" |
| 105 | + subnet_region = "us-east1" |
| 106 | + }, |
| 107 | + { |
| 108 | + subnet_name = "vpn-subnet-03" |
| 109 | + subnet_ip = "10.20.30.0/24" |
| 110 | + subnet_region = "europe-west4" |
| 111 | + } |
| 112 | + ] |
| 113 | +} |
| 114 | + |
| 115 | +module "vpn_spoke_local_vpc" { |
| 116 | + source = "terraform-google-modules/network/google" |
| 117 | + project_id = var.project_id |
| 118 | + network_name = var.vpn_spoke_local_vpc_name |
| 119 | + routing_mode = "GLOBAL" |
| 120 | + subnets = [] |
| 121 | +} |
| 122 | + |
| 123 | +module "remote_to_local_vpn" { |
| 124 | + source = "terraform-google-modules/vpn/google//modules/vpn_ha" |
| 125 | + version = "~> 4.0" |
| 126 | + |
| 127 | + project_id = var.project_id |
| 128 | + region = var.vpn_region |
| 129 | + network = module.vpn_spoke_remote_vpc.network_id |
| 130 | + name = "remote-to-local" |
| 131 | + router_asn = 64513 |
| 132 | + peer_gcp_gateway = module.local_to_remote_vpn.self_link |
| 133 | + tunnels = { |
| 134 | + remote-0 = { |
| 135 | + bgp_peer = { |
| 136 | + address = "169.254.1.2" |
| 137 | + asn = 64514 |
| 138 | + } |
| 139 | + bgp_peer_options = null |
| 140 | + bgp_session_range = "169.254.1.1/30" |
| 141 | + ike_version = 2 |
| 142 | + vpn_gateway_interface = 0 |
| 143 | + peer_external_gateway_interface = null |
| 144 | + shared_secret = module.local_to_remote_vpn.random_secret |
| 145 | + } |
| 146 | + remote-1 = { |
| 147 | + bgp_peer = { |
| 148 | + address = "169.254.2.2" |
| 149 | + asn = 64514 |
| 150 | + } |
| 151 | + bgp_peer_options = null |
| 152 | + bgp_session_range = "169.254.2.1/30" |
| 153 | + ike_version = 2 |
| 154 | + vpn_gateway_interface = 1 |
| 155 | + peer_external_gateway_interface = null |
| 156 | + shared_secret = module.local_to_remote_vpn.random_secret |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +module "local_to_remote_vpn" { |
| 162 | + source = "terraform-google-modules/vpn/google//modules/vpn_ha" |
| 163 | + version = "~> 4.0" |
| 164 | + |
| 165 | + project_id = var.project_id |
| 166 | + region = var.vpn_region |
| 167 | + network = module.vpn_spoke_local_vpc.network_id |
| 168 | + name = "local-to-remote" |
| 169 | + peer_gcp_gateway = module.remote_to_local_vpn.self_link |
| 170 | + router_asn = 64514 |
| 171 | + tunnels = { |
| 172 | + remote-0 = { |
| 173 | + bgp_peer = { |
| 174 | + address = "169.254.1.1" |
| 175 | + asn = 64513 |
| 176 | + } |
| 177 | + bgp_peer_options = null |
| 178 | + bgp_session_range = "169.254.1.2/30" |
| 179 | + ike_version = 2 |
| 180 | + vpn_gateway_interface = 0 |
| 181 | + peer_external_gateway_interface = null |
| 182 | + shared_secret = "" |
| 183 | + } |
| 184 | + remote-1 = { |
| 185 | + bgp_peer = { |
| 186 | + address = "169.254.2.1" |
| 187 | + asn = 64513 |
| 188 | + } |
| 189 | + bgp_peer_options = null |
| 190 | + bgp_session_range = "169.254.2.2/30" |
| 191 | + ike_version = 2 |
| 192 | + vpn_gateway_interface = 1 |
| 193 | + peer_external_gateway_interface = null |
| 194 | + shared_secret = "" |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +################################ |
| 201 | +# Router Appliance Spoke # |
| 202 | +################################ |
| 203 | +data "google_compute_zones" "available" { |
| 204 | + project = var.project_id |
| 205 | + region = var.instance_region |
| 206 | +} |
| 207 | + |
| 208 | +resource "random_shuffle" "zone" { |
| 209 | + input = data.google_compute_zones.available.names |
| 210 | + result_count = 1 |
| 211 | +} |
| 212 | + |
| 213 | +module "router_appliance_spoke_vpc" { |
| 214 | + source = "terraform-google-modules/network/google" |
| 215 | + project_id = var.project_id |
| 216 | + network_name = var.router_appliance_vpc_name |
| 217 | + routing_mode = "GLOBAL" |
| 218 | + |
| 219 | + subnets = [ |
| 220 | + { |
| 221 | + subnet_name = "router-appliance-subnet-01" |
| 222 | + subnet_ip = "10.20.10.0/24" |
| 223 | + subnet_region = var.instance_region |
| 224 | + } |
| 225 | + ] |
| 226 | +} |
| 227 | + |
| 228 | +resource "google_compute_instance" "router_appliance_1" { |
| 229 | + name = "fake-router-appliance-1" |
| 230 | + machine_type = "e2-medium" |
| 231 | + project = var.project_id |
| 232 | + can_ip_forward = true |
| 233 | + zone = random_shuffle.zone.result[0] |
| 234 | + |
| 235 | + boot_disk { |
| 236 | + initialize_params { |
| 237 | + image = "debian-cloud/debian-11" |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + network_interface { |
| 242 | + subnetwork = module.router_appliance_spoke_vpc.subnets["${var.instance_region}/router-appliance-subnet-01"].id |
| 243 | + access_config { |
| 244 | + network_tier = "PREMIUM" |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments