-
Notifications
You must be signed in to change notification settings - Fork 4
/
app-main.tf
382 lines (346 loc) · 14.1 KB
/
app-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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Accept the agreement for the mgmt-byol for vmspoke image
resource "azurerm_marketplace_agreement" "vmspoke-agreement" {
count = var.vmspoke-sku-enabled ? 0 : 1
publisher = var.vmspoke-publisher
offer = var.vmspoke-offer
plan = var.vmspoke-sku
}
resource "azurerm_resource_group" "rg-app-A" {
name = "rg-${var.app-name-con}"
location = var.location
}
resource "azurerm_public_ip" "publicip-app-A" {
name = "pub-${var.app-name-con}"
sku = "Standard"
location = azurerm_resource_group.rg-app-A.location
resource_group_name = azurerm_resource_group.rg-app-A.name
allocation_method = "Static"
domain_name_label = "pub-${var.app-name-con}-${var.mgmt-dns-suffix}"
}
resource "azurerm_lb" "lb-app-A" {
name = "lb-${var.app-name-con}"
sku = "Standard"
location = azurerm_resource_group.rg-app-A.location
resource_group_name = azurerm_resource_group.rg-app-A.name
frontend_ip_configuration {
name = "PublicIPAddress"
public_ip_address_id = azurerm_public_ip.publicip-app-A.id
gateway_load_balancer_frontend_ip_configuration_id = data.azurerm_lb.gateway-lb.frontend_ip_configuration.0.id
}
depends_on = [azurerm_resource_group_template_deployment.template-deployment-gwlb]
}
resource "azurerm_lb_backend_address_pool" "lb-backend-pool" {
loadbalancer_id = azurerm_lb.lb-app-A.id
name = "BackEndAddressPool"
depends_on = [azurerm_lb.lb-app-A]
}
resource "azurerm_lb_backend_address_pool_address" "lb-backend-pool-addr" {
name = "BackEndAddressPoolAddr"
backend_address_pool_id = azurerm_lb_backend_address_pool.lb-backend-pool.id
virtual_network_id = azurerm_virtual_network.vnet-spoke[0].id
ip_address = "10.0.0.4"
depends_on = [azurerm_lb_backend_address_pool.lb-backend-pool]
}
resource "azurerm_lb_probe" "lb-backend-probe" {
resource_group_name = azurerm_resource_group.rg-app-A.name
loadbalancer_id = azurerm_lb.lb-app-A.id
name = "http-running-probe"
port = 3000
depends_on = [azurerm_lb.lb-app-A]
}
resource "azurerm_lb_rule" "lb-rule-3000" {
resource_group_name = azurerm_resource_group.rg-app-A.name
loadbalancer_id = azurerm_lb.lb-app-A.id
name = "LBRule3000"
protocol = "Tcp"
frontend_port = 3000
backend_port = 3000
frontend_ip_configuration_name = "PublicIPAddress"
backend_address_pool_ids = [azurerm_lb_backend_address_pool.lb-backend-pool.id]
probe_id = azurerm_lb_probe.lb-backend-probe.id
depends_on = [azurerm_lb.lb-app-A]
}
resource "azurerm_lb_rule" "lb-rule-80" {
resource_group_name = azurerm_resource_group.rg-app-A.name
loadbalancer_id = azurerm_lb.lb-app-A.id
name = "LBRule80"
protocol = "Tcp"
frontend_port = 80
backend_port = 80
frontend_ip_configuration_name = "PublicIPAddress"
backend_address_pool_ids = [azurerm_lb_backend_address_pool.lb-backend-pool.id]
probe_id = azurerm_lb_probe.lb-backend-probe.id
depends_on = [azurerm_lb.lb-app-A]
}
resource "azurerm_network_profile" "profile-app-juiceshop" {
name = "net-profile-juiceshop-${var.app-name-con}"
location = azurerm_resource_group.rg-app-A.location
resource_group_name = azurerm_resource_group.rg-app-A.name
container_network_interface {
name = "container-nic-${var.app-name-con}"
ip_configuration {
name = "nic-ipconfig-${var.app-name-con}"
subnet_id = azurerm_subnet.net-spoke-0-web.id
}
}
depends_on = [azurerm_subnet.net-spoke-0-web]
}
resource "azurerm_container_group" "container-app-juiceshop" {
name = "juiceshop-${var.app-name-con}"
location = azurerm_resource_group.rg-app-A.location
resource_group_name = azurerm_resource_group.rg-app-A.name
os_type = "Linux"
ip_address_type = "private"
network_profile_id = azurerm_network_profile.profile-app-juiceshop.id
container {
name = "juiceshop${var.app-name-con}"
image = "${var.docker-image}:latest"
cpu = "1"
memory = "1.5"
ports {
port = 3000
protocol = "TCP"
}
ports {
port = 80
protocol = "TCP"
}
}
depends_on = [azurerm_network_profile.profile-app-juiceshop]
}
# Building the second app
resource "azurerm_resource_group" "rg-app-B" {
name = "rg-${var.app-name-vm}"
location = var.location
}
resource "azurerm_public_ip" "publicip-app-B" {
name = "pub-${var.app-name-vm}"
sku = "Standard"
location = azurerm_resource_group.rg-app-B.location
resource_group_name = azurerm_resource_group.rg-app-B.name
allocation_method = "Static"
domain_name_label = "pub-${var.app-name-vm}-${var.mgmt-dns-suffix}"
}
resource "azurerm_lb" "lb-app-B" {
name = "lb-${var.app-name-vm}"
sku = "Standard"
location = azurerm_resource_group.rg-app-B.location
resource_group_name = azurerm_resource_group.rg-app-B.name
frontend_ip_configuration {
name = "PublicIPAddress"
public_ip_address_id = azurerm_public_ip.publicip-app-B.id
gateway_load_balancer_frontend_ip_configuration_id = data.azurerm_lb.gateway-lb.frontend_ip_configuration.0.id
}
depends_on = [azurerm_resource_group_template_deployment.template-deployment-gwlb]
}
resource "azurerm_lb_backend_address_pool" "lb-backend-pool-B" {
loadbalancer_id = azurerm_lb.lb-app-B.id
name = "BackEndAddressPool"
depends_on = [azurerm_lb.lb-app-B]
}
resource "azurerm_lb_backend_address_pool_address" "lb-backend-pool-B-addr" {
name = "BackEndAddressPoolAddr"
backend_address_pool_id = azurerm_lb_backend_address_pool.lb-backend-pool-B.id
virtual_network_id = azurerm_virtual_network.vnet-spoke[1].id
ip_address = "10.0.4.4"
depends_on = [azurerm_lb_backend_address_pool.lb-backend-pool-B]
}
resource "azurerm_lb_probe" "lb-backend-probe-B" {
resource_group_name = azurerm_resource_group.rg-app-B.name
loadbalancer_id = azurerm_lb.lb-app-B.id
name = "http-running-probe"
port = 80
depends_on = [azurerm_lb.lb-app-B]
}
resource "azurerm_lb_rule" "lb-rule-B-80" {
resource_group_name = azurerm_resource_group.rg-app-B.name
loadbalancer_id = azurerm_lb.lb-app-B.id
name = "LBRule80"
protocol = "Tcp"
frontend_port = 80
backend_port = 80
frontend_ip_configuration_name = "PublicIPAddress"
backend_address_pool_ids = [azurerm_lb_backend_address_pool.lb-backend-pool-B.id]
probe_id = azurerm_lb_probe.lb-backend-probe-B.id
depends_on = [azurerm_lb.lb-app-B]
}
# VM-Spoke Network interface
resource "azurerm_network_interface" "nic-vmspoke" {
name = "${var.app-name-vm}-eth0"
location = azurerm_resource_group.rg-app-B.location
resource_group_name = azurerm_resource_group.rg-app-B.name
enable_ip_forwarding = "false"
ip_configuration {
name = "${var.app-name-vm}-eth0-config"
subnet_id = azurerm_subnet.net-spoke-1-web.id
private_ip_address_allocation = "Dynamic"
primary = true
}
depends_on = [azurerm_subnet.net-spoke-1-web]
}
# Create NSG for the vmspoke
resource "azurerm_network_security_group" "nsg-vmspoke" {
name = "nsg-vmspoke"
location = azurerm_resource_group.rg-app-B.location
resource_group_name = azurerm_resource_group.rg-app-B.name
}
# Create the NSG rules for the vmspoke
resource "azurerm_network_security_rule" "nsg-vmspoke-rl-http-s" {
priority = 110
name = "http-s-access"
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_ranges = ["80","443"]
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.rg-app-B.name
network_security_group_name = azurerm_network_security_group.nsg-vmspoke.name
depends_on = [azurerm_network_security_group.nsg-vmspoke]
}
resource "azurerm_network_interface_security_group_association" "nsg-assoc-nic-vmspoke" {
network_interface_id = azurerm_network_interface.nic-vmspoke.id
network_security_group_id = azurerm_network_security_group.nsg-vmspoke.id
depends_on = [azurerm_network_interface.nic-vmspoke,azurerm_network_security_group.nsg-vmspoke]
}
# Accept the agreement for the mgmt-byol for vmspoke image
resource "azurerm_marketplace_agreement" "vmspoke-agreement" {
count = var.vmspoke-sku-enabled ? 0 : 1
publisher = var.vmspoke-publisher
offer = var.vmspoke-offer
plan = var.vmspoke-sku
}
# VM-Spoke Virtual Machine
resource "azurerm_virtual_machine" "vm-spoke" {
name = "${var.app-name-vm}"
location = azurerm_resource_group.rg-app-B.location
resource_group_name = azurerm_resource_group.rg-app-B.name
network_interface_ids = [azurerm_network_interface.nic-vmspoke.id]
vm_size = "Standard_A1_v2"
plan {
publisher = var.vmspoke-publisher
product = var.vmspoke-offer
name = var.vmspoke-sku
}
storage_image_reference {
publisher = var.vmspoke-publisher
offer = var.vmspoke-offer
sku = var.vmspoke-sku
version = "latest"
}
storage_os_disk {
name = "disk-${var.app-name-vm}"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "${var.app-name-vm}"
admin_username = var.chkp-admin-usr
admin_password = var.chkp-admin-pwd
}
os_profile_linux_config {
disable_password_authentication = false
}
depends_on = [azurerm_marketplace_agreement.vmspoke-agreement,azurerm_network_interface.nic-vmspoke]
}
resource "azurerm_resource_group" "rg-app-C" {
name = "rg-${var.app-name-direct}"
location = var.location
}
resource "azurerm_public_ip" "publicip-app-C" {
name = "pub-${var.app-name-direct}"
sku = "Standard"
location = azurerm_resource_group.rg-app-C.location
resource_group_name = azurerm_resource_group.rg-app-C.name
allocation_method = "Static"
domain_name_label = "pub-${var.app-name-direct}-${var.mgmt-dns-suffix}"
}
resource "azurerm_network_interface" "nic-vmspoke-C" {
name = "${var.app-name-direct}-eth0"
location = azurerm_resource_group.rg-app-C.location
resource_group_name = azurerm_resource_group.rg-app-C.name
enable_ip_forwarding = "false"
ip_configuration {
name = "${var.app-name-direct}-eth0-config"
subnet_id = azurerm_subnet.net-spoke-1-web.id
private_ip_address_allocation = "Dynamic"
primary = true
public_ip_address_id = azurerm_public_ip.publicip-app-C.id
gateway_load_balancer_frontend_ip_configuration_id = data.azurerm_lb.gateway-lb.frontend_ip_configuration.0.id
}
depends_on = [azurerm_subnet.net-spoke-1-web]
}
resource "azurerm_network_interface_security_group_association" "nsg-assoc-nic-vmspoke-C" {
network_interface_id = azurerm_network_interface.nic-vmspoke-C.id
network_security_group_id = azurerm_network_security_group.nsg-vmspoke.id
depends_on = [azurerm_network_interface.nic-vmspoke-C,azurerm_network_security_group.nsg-vmspoke]
}
# VM-Spoke Virtual Machine
resource "azurerm_virtual_machine" "vm-spoke-C" {
name = var.app-name-direct
location = azurerm_resource_group.rg-app-C.location
resource_group_name = azurerm_resource_group.rg-app-C.name
network_interface_ids = [azurerm_network_interface.nic-vmspoke-C.id]
vm_size = "Standard_A1_v2"
plan {
publisher = var.vmspoke-publisher
product = var.vmspoke-offer
name = var.vmspoke-sku
}
storage_image_reference {
publisher = var.vmspoke-publisher
offer = var.vmspoke-offer
sku = var.vmspoke-sku
version = "latest"
}
storage_os_disk {
name = "disk-${var.app-name-direct}"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "${var.app-name-direct}"
admin_username = var.chkp-admin-usr
admin_password = var.chkp-admin-pwd
}
os_profile_linux_config {
disable_password_authentication = false
}
depends_on = [azurerm_marketplace_agreement.vmspoke-agreement,azurerm_network_interface.nic-vmspoke-C]
}
resource "azurerm_dns_a_record" "publicip-app-A-dns-record" {
name = var.app-name-con
zone_name = azurerm_dns_zone.mydns-public-zone.name
resource_group_name = azurerm_resource_group.rg-dns-myzone.name
ttl = 300
target_resource_id = azurerm_public_ip.publicip-app-A.id
depends_on = [azurerm_public_ip.publicip-app-A]
}
output "webapp-A-public-fqdn" {
value = "http://${azurerm_dns_a_record.publicip-app-A-dns-record.name}.${azurerm_dns_zone.mydns-public-zone.name}:3000/"
}
resource "azurerm_dns_a_record" "publicip-app-B-dns-record" {
name = var.app-name-vm
zone_name = azurerm_dns_zone.mydns-public-zone.name
resource_group_name = azurerm_resource_group.rg-dns-myzone.name
ttl = 300
target_resource_id = azurerm_public_ip.publicip-app-B.id
depends_on = [azurerm_public_ip.publicip-app-B]
}
output "webapp-B-public-fqdn" {
value = "http://${azurerm_dns_a_record.publicip-app-B-dns-record.name}.${azurerm_dns_zone.mydns-public-zone.name}/"
}
resource "azurerm_dns_a_record" "publicip-app-C-dns-record" {
name = var.app-name-direct
zone_name = azurerm_dns_zone.mydns-public-zone.name
resource_group_name = azurerm_resource_group.rg-dns-myzone.name
ttl = 300
target_resource_id = azurerm_public_ip.publicip-app-C.id
depends_on = [azurerm_public_ip.publicip-app-C]
}
output "webapp-C-public-fqdn" {
value = "http://${azurerm_dns_a_record.publicip-app-C-dns-record.name}.${azurerm_dns_zone.mydns-public-zone.name}/"
}