-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
140 lines (130 loc) · 5.13 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
locals {
region = var.region
project_name = var.project_name
environment = var.environment
name = var.name
}
resource "google_compute_network" "network" {
name = "${var.name}-vpc"
auto_create_subnetworks = var.auto_create_subnetworks
routing_mode = var.routing_mode
project = var.project_name
delete_default_routes_on_create = var.delete_default_internet_gateway_routes
mtu = var.mtu
}
module "subnets" {
depends_on = [google_compute_network.network]
source = "./modules/subnets"
name = format("%s-%s-subnet", var.environment, var.name)
ip_cidr_range = var.ip_cidr_range
private_ip_google_access = var.private_ip_google_access
private_ipv6_google_access = var.private_ipv6_google_access
region = var.region
secondary_ip_range = var.secondary_ip_range
network_name = google_compute_network.network.self_link
project_id = local.project_name
flow_logs = var.vpc_flow_logs
log_config = var.log_config
}
resource "google_compute_router" "router" {
count = var.enable_nat_gateway ? 1 : 0
project = local.project_name
depends_on = [google_compute_network.network]
name = format("%s-%s-router", local.name, local.environment)
network = google_compute_network.network.self_link
region = local.region
}
module "cloud-nat" {
depends_on = [google_compute_network.network]
source = "terraform-google-modules/cloud-nat/google"
version = "4.0.0"
count = var.enable_nat_gateway ? 1 : 0
project_id = local.project_name
region = local.region
router = google_compute_router.router[0].name
name = format("%s-%s-nat", local.name, local.environment)
source_subnetwork_ip_ranges_to_nat = var.source_subnetwork_ip_ranges_to_nat
log_config_enable = var.vpc_flow_logs
log_config_filter = var.log_config_filter_nat
min_ports_per_vm = "128"
icmp_idle_timeout_sec = "30"
tcp_established_idle_timeout_sec = "1200"
tcp_transitory_idle_timeout_sec = "30"
udp_idle_timeout_sec = "30"
}
module "firewall_rules" {
source = "terraform-google-modules/network/google//modules/firewall-rules"
version = "~> 7.0"
project_id = local.project_name
network_name = google_compute_network.network.self_link
depends_on = [google_compute_network.network]
rules = [
{
name = format("%s-%s-http-allow", local.name, local.environment)
description = null
direction = "INGRESS"
priority = null
ranges = ["0.0.0.0/0"]
source_tags = null
source_service_accounts = null
target_tags = ["http-server"]
target_service_accounts = null
allow = [{
protocol = "tcp"
ports = ["80"]
}]
deny = []
log_config = {
metadata = "INCLUDE_ALL_METADATA"
}
},
{
name = format("%s-%s-https-allow", local.name, local.environment)
description = null
direction = "INGRESS"
priority = null
ranges = ["0.0.0.0/0"]
source_tags = null
source_service_accounts = null
target_tags = ["https-server"]
target_service_accounts = null
allow = [{
protocol = "tcp"
ports = ["443"]
}]
deny = []
log_config = {
metadata = "INCLUDE_ALL_METADATA"
}
},
]
}
resource "google_compute_global_address" "private_ip_block" {
count = var.db_private_access ? 1 : 0
project = local.project_name
name = format("%s-%s-private-ip-block", local.name, local.environment)
purpose = "VPC_PEERING"
address_type = "INTERNAL"
ip_version = "IPV4"
prefix_length = 20
network = google_compute_network.network.self_link
}
resource "google_service_networking_connection" "private_vpc_connection" {
count = var.db_private_access ? 1 : 0
depends_on = [google_compute_global_address.private_ip_block]
network = google_compute_network.network.self_link
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.private_ip_block[count.index].name]
}
module "vpn_server" {
depends_on = [module.subnets]
source = "./modules/vpn"
count = var.create_vpn ? 1 : 0
project_name = local.project_name
name = local.name
environment = local.environment
zone = format("%s-a", var.region)
network_name = google_compute_network.network.self_link
subnetwork = module.subnets.subnet_name
machine_type = var.machine_type
}