-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.tf
66 lines (57 loc) · 2.77 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Copyright 2018 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
*
* http://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.
*/
locals {
tunnel_name_prefix = var.tunnel_name_prefix != "" ? var.tunnel_name_prefix : "${var.network}-${var.gateway_name}-tunnel"
default_shared_secret = var.shared_secret != "" ? var.shared_secret : random_id.ipsec_secret.b64_url
}
# For VPN gateways with static routing
## Create Route (for static routing gateways)
resource "google_compute_route" "route" {
count = !var.cr_enabled ? var.tunnel_count * length(var.remote_subnet) : 0
name = "${google_compute_vpn_gateway.vpn_gateway.name}-tunnel${floor(count.index / length(var.remote_subnet)) + 1}-route${count.index % length(var.remote_subnet) + 1}"
network = var.network
project = var.project_id
dest_range = var.remote_subnet[count.index % length(var.remote_subnet)]
priority = var.route_priority
tags = var.route_tags
next_hop_vpn_tunnel = google_compute_vpn_tunnel.tunnel-static[floor(count.index / length(var.remote_subnet))].self_link
depends_on = [google_compute_vpn_tunnel.tunnel-static]
}
# For VPN gateways routing through BGP and Cloud Routers
## Create Router Interfaces
resource "google_compute_router_interface" "router_interface" {
count = var.cr_enabled ? var.tunnel_count : 0
name = "interface-${local.tunnel_name_prefix}-${count.index}"
router = var.cr_name
region = var.region
ip_range = var.bgp_cr_session_range[count.index]
vpn_tunnel = google_compute_vpn_tunnel.tunnel-dynamic[count.index].name
project = var.project_id
depends_on = [google_compute_vpn_tunnel.tunnel-dynamic]
}
## Create Peers
resource "google_compute_router_peer" "bgp_peer" {
count = var.cr_enabled ? var.tunnel_count : 0
name = "bgp-session-${local.tunnel_name_prefix}-${count.index}"
router = var.cr_name
region = var.region
peer_ip_address = var.bgp_remote_session_range[count.index]
peer_asn = var.peer_asn[count.index]
advertised_route_priority = var.advertised_route_priority
interface = "interface-${local.tunnel_name_prefix}-${count.index}"
project = var.project_id
depends_on = [google_compute_router_interface.router_interface]
}