forked from oracle-terraform-modules/terraform-oci-vcn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcn_gateways.tf
327 lines (259 loc) · 12.3 KB
/
vcn_gateways.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Copyright (c) 2019, 2021, Oracle Corporation and/or affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
########################
# Internet Gateway (IGW)
########################
resource "oci_core_internet_gateway" "ig" {
compartment_id = var.compartment_id
display_name = var.label_prefix == "none" ? var.internet_gateway_display_name : "${var.label_prefix}-${var.internet_gateway_display_name}"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
vcn_id = oci_core_vcn.vcn.id
lifecycle {
ignore_changes = [defined_tags, freeform_tags]
}
count = var.create_internet_gateway == true ? 1 : 0
}
resource "oci_core_route_table" "ig" {
compartment_id = var.compartment_id
display_name = var.label_prefix == "none" ? "internet-route" : "${var.label_prefix}-internet-route"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
route_rules {
# * With this route table, Internet Gateway is always declared as the default gateway
destination = local.anywhere
network_entity_id = oci_core_internet_gateway.ig[0].id
description = "Terraformed - Auto-generated at Internet Gateway creation: Internet Gateway as default gateway"
}
dynamic "route_rules" {
# * With this route table, Internet Gateway is always declared as the default gateway
for_each = var.enable_ipv6 == true ? [1] : []
content {
destination = local.anywhere_ipv6
network_entity_id = oci_core_internet_gateway.ig[0].id
description = "Terraformed - Auto-generated at Internet Gateway creation: Internet Gateway as default gateway"
}
}
dynamic "route_rules" {
# * filter var.internet_gateway_route_rules for routes with "drg" as destination
# * and steer traffic to the attached DRG if available
for_each = var.internet_gateway_route_rules != null ? { for k, v in var.internet_gateway_route_rules : k => v
if v.network_entity_id == "drg" && var.attached_drg_id != null } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = var.attached_drg_id
description = route_rules.value.description
}
}
dynamic "route_rules" {
# * filter var.internet_gateway_route_rules for routes with "internet_gateway" as destination
# * and steer traffic to the module created Internet Gateway
for_each = var.internet_gateway_route_rules != null ? { for k, v in var.internet_gateway_route_rules : k => v
if v.network_entity_id == "internet_gateway" } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = oci_core_internet_gateway.ig[0].id
description = route_rules.value.description
}
}
dynamic "route_rules" {
# * filter var.internet_gateway_route_rules for routes with "lpg@" as destination
# * and steer traffic to the attached LPG if available
for_each = var.internet_gateway_route_rules != null ? { for k, v in var.internet_gateway_route_rules : k => v
if startswith(v.network_entity_id, "lpg@") && var.local_peering_gateways != null } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = oci_core_local_peering_gateway.lpg[split("@", route_rules.value.network_entity_id)[1]].id
description = route_rules.value.description
}
}
dynamic "route_rules" {
# * filter var.internet_gateway_route_rules for generic routes
# * can take any Named Value : String, Input Variable, Local Value, Data Source, Resource, Module Output ...
# * useful for gateways that are not managed by the module
for_each = var.internet_gateway_route_rules != null ? { for k, v in var.internet_gateway_route_rules : k => v
if contains(["drg", "internet_gateway"], v.network_entity_id) == false && startswith(v.network_entity_id, "lpg@") == false } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = route_rules.value.network_entity_id
description = route_rules.value.description
}
}
vcn_id = oci_core_vcn.vcn.id
lifecycle {
ignore_changes = [defined_tags, freeform_tags]
}
count = var.create_internet_gateway == true ? 1 : 0
}
#######################
# Service Gateway (SGW)
#######################
data "oci_core_services" "all_oci_services" {
filter {
name = "name"
values = ["All .* Services In Oracle Services Network"]
regex = true
}
count = var.create_service_gateway == true ? 1 : 0
}
resource "oci_core_service_gateway" "service_gateway" {
compartment_id = var.compartment_id
display_name = var.label_prefix == "none" ? var.service_gateway_display_name : "${var.label_prefix}-${var.service_gateway_display_name}"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
services {
service_id = lookup(data.oci_core_services.all_oci_services[0].services[0], "id")
}
vcn_id = oci_core_vcn.vcn.id
lifecycle {
ignore_changes = [defined_tags, freeform_tags]
}
count = var.create_service_gateway == true ? 1 : 0
}
resource "oci_core_route_table" "service_gw" {
compartment_id = var.compartment_id
display_name = var.label_prefix == "none" ? "service-gw-route" : "${var.label_prefix}-service-gw-route"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
dynamic "route_rules" {
# * If Service Gateway is created with the module, automatically creates a rule to handle traffic for "all services" through Service Gateway
for_each = var.create_service_gateway == true ? [1] : []
content {
destination = lookup(data.oci_core_services.all_oci_services[0].services[0], "cidr_block")
destination_type = "SERVICE_CIDR_BLOCK"
network_entity_id = oci_core_service_gateway.service_gateway[0].id
description = "Terraformed - Auto-generated at Service Gateway creation: All Services in region to Service Gateway"
}
}
vcn_id = oci_core_vcn.vcn.id
lifecycle {
ignore_changes = [defined_tags, freeform_tags]
}
count = var.create_service_gateway == true ? 1 : 0
}
###################
# NAT Gateway (NGW)
###################
resource "oci_core_public_ip" "nat_gateway_public_ip" {
count = var.create_nat_gateway == true && var.nat_gateway_public_ip_id == "RESERVED" ? 1 : 0
compartment_id = var.compartment_id
lifetime = "RESERVED"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
display_name = var.label_prefix == "none" ? var.nat_gateway_display_name : "${var.label_prefix}-${var.nat_gateway_display_name}"
}
resource "oci_core_nat_gateway" "nat_gateway" {
compartment_id = var.compartment_id
display_name = var.label_prefix == "none" ? var.nat_gateway_display_name : "${var.label_prefix}-${var.nat_gateway_display_name}"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
public_ip_id = var.nat_gateway_public_ip_id != "none" ? var.nat_gateway_public_ip_id != "RESERVED" ? var.nat_gateway_public_ip_id : join(",",oci_core_public_ip.nat_gateway_public_ip.*.id) : null
vcn_id = oci_core_vcn.vcn.id
lifecycle {
ignore_changes = [defined_tags, freeform_tags]
}
count = var.create_nat_gateway == true ? 1 : 0
}
# special fix due to bug introduced in #101 which causes destruction and recreation of subnets
# for existing users
resource "oci_core_route_table" "nat" {
compartment_id = var.compartment_id
display_name = var.label_prefix == "none" ? "nat-route" : "${var.label_prefix}-nat-route"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
route_rules {
# * With this route table, NAT Gateway is always declared as the default gateway
destination = local.anywhere
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_nat_gateway.nat_gateway[0].id
description = "Terraformed - Auto-generated at NAT Gateway creation: NAT Gateway as default gateway"
}
# bring this block back to fix #101
dynamic "route_rules" {
# * If Service Gateway is created with the module, automatically creates a rule to handle traffic for "all services" through Service Gateway
for_each = var.create_service_gateway == true ? [1] : []
content {
destination = lookup(data.oci_core_services.all_oci_services[0].services[0], "cidr_block")
destination_type = "SERVICE_CIDR_BLOCK"
network_entity_id = oci_core_service_gateway.service_gateway[0].id
description = "Terraformed - Auto-generated at Service Gateway creation: All Services in region to Service Gateway"
}
}
dynamic "route_rules" {
# * filter var.nat_gateway_route_rules for routes with "drg" as destination
# * and steer traffic to the attached DRG if available
for_each = var.nat_gateway_route_rules != null ? { for k, v in var.nat_gateway_route_rules : k => v
if v.network_entity_id == "drg" && var.attached_drg_id != null } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = var.attached_drg_id
description = route_rules.value.description
}
}
dynamic "route_rules" {
# * filter var.nat_gateway_route_rules for routes with "nat_gateway" as destination
# * and steer traffic to the module created NAT Gateway
for_each = var.nat_gateway_route_rules != null ? { for k, v in var.nat_gateway_route_rules : k => v
if v.network_entity_id == "nat_gateway" } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = oci_core_nat_gateway.nat_gateway[0].id
description = route_rules.value.description
}
}
dynamic "route_rules" {
# * filter var.nat_gateway_route_rules for routes with "lpg@" as destination
# * and steer traffic to the attached LPG if available
for_each = var.nat_gateway_route_rules != null ? { for k, v in var.nat_gateway_route_rules : k => v
if startswith(v.network_entity_id, "lpg@") && var.local_peering_gateways != null } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = oci_core_local_peering_gateway.lpg[split("@", route_rules.value.network_entity_id)[1]].id
description = route_rules.value.description
}
}
dynamic "route_rules" {
# * filter var.nat_gateway_route_rules for generic routes
# * can take any Named Value : String, Input Variable, Local Value, Data Source, Resource, Module Output ...
# * useful for gateways that are not managed by the module
for_each = var.nat_gateway_route_rules != null ? { for k, v in var.nat_gateway_route_rules : k => v
if contains(["drg", "nat_gateway"], v.network_entity_id) == false && startswith(v.network_entity_id, "lpg@") == false } : {}
content {
destination = route_rules.value.destination
destination_type = route_rules.value.destination_type
network_entity_id = route_rules.value.network_entity_id
description = route_rules.value.description
}
}
vcn_id = oci_core_vcn.vcn.id
# ignore changes to route rules to avoid recreation issues due to #101.
# A fix may still be needed for when new custom route rules are added.
lifecycle {
ignore_changes = [defined_tags, freeform_tags]
}
count = var.create_nat_gateway ? 1 : 0
}
#############################
# Local Peering Gateway (LPG)
#############################
resource "oci_core_local_peering_gateway" "lpg" {
for_each = var.local_peering_gateways != null ? var.local_peering_gateways : {}
compartment_id = var.compartment_id
display_name = var.label_prefix == "none" ? each.key : "${var.label_prefix}-${each.key}"
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
vcn_id = oci_core_vcn.vcn.id
#Optional
peer_id = can(each.value.peer_id) == false ? null : each.value.peer_id
route_table_id = can(each.value.route_table_id) == false ? null : each.value.route_table_id
lifecycle {
ignore_changes = [defined_tags, freeform_tags]
}
}