-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
1524 lines (1376 loc) · 64.3 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#################
# Azure provider
#################
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
#####################
# Azure subscription
#####################
data "azurerm_subscription" "current" {
}
provider "azurerm" {
features {}
subscription_id = var.subscription_id == "" ? null : var.subscription_id
tenant_id = var.tenant_id == "" ? null : var.client_secret
client_id = var.client_id == "" ? null : var.client_id
client_secret = var.client_secret == "" ? null : var.client_secret
}
##################
# Local variables
##################
locals {
os = data.external.os.result.os
resource_group = (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].name
: azurerm_resource_group.resource_group[0].name
)
location = (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].location
: var.location
)
subnet_name = (
var.subnet_exists == true
? data.azurerm_subnet.subnet[0].name
: azurerm_subnet.subnet[0].name
)
subnet_id = (
var.subnet_exists == true
? data.azurerm_subnet.subnet[0].id
: azurerm_subnet.subnet[0].id
)
# NSG
nsg_size = length(var.nsg_rules)
nsg_priority_base = 1000
# Additional DVDs
additional_dvds_list = try(
[tostring(var.additional_dvds)],
tolist(var.additional_dvds),
)
}
#########################
# Cloud-init definitions
#########################
data "template_file" "config" {
for_each = {
for vm in var.vms
: vm.name => vm
}
template = file(each.value.cloudinit)
vars = {
vm_number = length(var.vms)
vm_name = each.key
vm_names = join(" ", [for k, v in var.vms: v.name])
vm_ip = each.value.priv_ip_addr
vm_ips = join(" ", [for k, v in var.vms: v.priv_ip_addr])
vm_ip_network = cidrhost(var.subnet_addr, 0)
vm_swap_size = each.value.swap_size
vm_os_registration = each.value.os_registration
vm_reg_code = each.value.reg_code
vm_reg_email = each.value.reg_email
vm_product_id = each.value.product_id
vm_saptune_solution = each.value.saptune_solution
vm_sap_instance = each.value.sap_instance
admin_password = var.admin_password
sap_media_stg_enabled = var.sap_media_stg_enabled
sap_media_stg_type = var.sap_media_stg_type
sap_media_stg_account = var.sap_media_stg_account
sap_media_local_mount = var.sap_media_local_mount
sap_media_stg_remote_path = replace(var.sap_media_stg_remote_path, "/", "")
sap_media_stg_access_key = (
var.sap_media_stg_enabled == true
? (var.sap_media_stg_exists == true
? var.sap_media_stg_access_key
: azurerm_storage_account.stg_sap_media[0].primary_access_key
)
: ""
)
sap_media_local_add_fstab = var.sap_media_local_add_fstab
sap_media_pe_private_ip = (
var.sap_media_stg_enabled == true && var.sap_media_stg_pe_enabled == true
? (
var.sap_media_stg_pe_exists == true
? data.azurerm_private_endpoint_connection.pe_stg_sap_media[0].private_service_connection[0].private_ip_address
: azurerm_private_endpoint.pe_stg_sap_media[0].private_service_connection[0].private_ip_address
)
: ""
)
data_stg_enabled = var.data_stg_enabled
data_stg_account = var.data_stg_account
data_stg_remote_path = replace(var.data_stg_remote_path, "/", "")
data_stg_remote_clean = var.data_stg_remote_clean
data_stg_local_add_fstab = var.data_stg_local_add_fstab
data_stg_pe_private_ip = (
var.data_stg_enabled == true && var.data_stg_pe_enabled == true
? (
var.data_stg_pe_exists == true
? data.azurerm_private_endpoint_connection.pe_stg_data[0].private_service_connection[0].private_ip_address
: azurerm_private_endpoint.pe_stg_data[0].private_service_connection[0].private_ip_address
)
: ""
)
saptrans_stg_enabled = var.saptrans_stg_enabled
saptrans_stg_type = var.saptrans_stg_type
saptrans_stg_account = var.saptrans_stg_account
saptrans_stg_remote_path = (
var.saptrans_stg_type == "stga"
? replace(var.saptrans_stg_remote_path, "/", "")
: var.saptrans_stg_remote_path
)
saptrans_stg_remote_clean = var.saptrans_stg_remote_clean
saptrans_stg_local_add_fstab = var.saptrans_stg_local_add_fstab
saptrans_stg_pe_private_ip = (
var.saptrans_stg_enabled == true && var.saptrans_stg_pe_enabled == true
? (
var.saptrans_stg_pe_exists == true
? data.azurerm_private_endpoint_connection.pe_stg_saptrans[0].private_service_connection[0].private_ip_address
: azurerm_private_endpoint.pe_stg_saptrans[0].private_service_connection[0].private_ip_address
)
: ""
)
enable_monitoring = var.enable_monitoring
cluster_install = var.cluster_install
cluster_unicast = var.cluster_unicast
cluster_password = var.cluster_password
subscription_id = data.azurerm_subscription.current.subscription_id
resource_group = local.resource_group
tenant_id = data.azurerm_subscription.current.tenant_id
login_id = (
var.fencing_enabled == true
? (var.fencing_app_exists == false
? azuread_application.azuread_app[0].application_id
: data.azuread_application.azuread_app[0].application_id
)
: ""
)
app_password = (
var.fencing_enabled == true
? (var.fencing_app_exists == false
? azuread_application_password.azuread_app_password[0].value
: var.fencing_app_secret
)
: ""
)
sap_ascs_instance_sid = var.sap_ascs_instance_sid
sap_ascs_instance_id = var.sap_ascs_instance_id
sap_ascs_root_user = var.sap_ascs_root_user
sap_ascs_root_password = var.sap_ascs_root_password
sap_ascs_vip_address = var.sap_ascs_vip_address
sap_ascs_vip_hostname = var.sap_ascs_vip_hostname
sap_ers_instance_sid = var.sap_ers_instance_sid
sap_ers_instance_id = var.sap_ers_instance_id
sap_ers_root_user = var.sap_ers_root_user
sap_ers_root_password = var.sap_ers_root_password
sap_ers_vip_address = var.sap_ers_vip_address
sap_ers_vip_hostname = var.sap_ers_vip_hostname
sid_adm_password = var.sid_adm_password
sap_adm_password = var.sap_adm_password
master_password = var.master_password
sapmnt_path = var.sapmnt_path
sidadm_user_uid = var.sidadm_user_uid
sidadm_user_gid = var.sidadm_user_gid
swpm_folder = var.swpm_folder
sapexe_folder = var.sapexe_folder
sapmnt_inst_media = var.sapmnt_inst_media
additional_dvds = join(" ", [for path in local.additional_dvds_list : tostring(path)])
sap_hana_host = var.sap_hana_host
sap_hana_ip = var.sap_hana_ip
sap_hana_sid = var.sap_hana_sid
sap_hana_instance = var.sap_hana_instance
sap_hana_password = var.sap_hana_password
}
}
data "template_cloudinit_config" "config" {
for_each = {
for vm in var.vms
: vm.name => vm
}
gzip = true
base64_encode = true
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = data.template_file.config[each.key].rendered
}
}
################################
# Azure instance resource group
################################
data "azurerm_resource_group" "resource_group" {
count = var.rg_exists == true ? 1 : 0
name = var.resource_group
}
resource "azurerm_resource_group" "resource_group" {
count = var.rg_exists == false ? 1 : 0
name = var.resource_group
location = local.location
tags = var.tags
}
#############
# Azure vnet
#############
data "azurerm_virtual_network" "virtual_network" {
count = var.vnet_exists == true ? 1 : 0
name = var.vnet_name
resource_group_name = var.vnet_rg
}
# Create virtual network
resource "azurerm_virtual_network" "virtual_network" {
count = var.vnet_exists == false ? 1 : 0
name = var.vnet_name
address_space = [var.vnet_addr]
location = local.location
resource_group_name = (
var.resource_group == var.vnet_rg
? (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].name
: azurerm_resource_group.resource_group[0].name
)
: var.vnet_rg
)
tags = var.tags
}
# Use existing subnet network
data "azurerm_subnet" "subnet" {
count = var.subnet_exists == true ? 1 : 0
name = var.subnet_name
resource_group_name = (
var.vnet_exists == true
? data.azurerm_virtual_network.virtual_network[0].resource_group_name
: azurerm_virtual_network.virtual_network[0].resource_group_name
)
virtual_network_name = (
var.vnet_exists == true
? data.azurerm_virtual_network.virtual_network[0].name
: azurerm_virtual_network.virtual_network[0].name
)
}
# Create subnet
resource "azurerm_subnet" "subnet" {
count = var.subnet_exists == false ? 1 : 0
name = var.subnet_name
resource_group_name = (
var.vnet_exists == true
? data.azurerm_virtual_network.virtual_network[0].resource_group_name
: azurerm_virtual_network.virtual_network[0].resource_group_name
)
virtual_network_name = (
var.vnet_exists == true
? data.azurerm_virtual_network.virtual_network[0].name
: azurerm_virtual_network.virtual_network[0].name
)
address_prefixes = [var.subnet_addr]
service_endpoints = ["Microsoft.Storage"]
private_endpoint_network_policies_enabled = true
private_link_service_network_policies_enabled = true
}
######################
# AzureAD Application
######################
data "azuread_client_config" "current" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
}
resource "random_uuid" "fencing_app_oauth2_permission_scope" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
}
resource "random_uuid" "fencing_app_app_role" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
}
data "azuread_application" "azuread_app" {
count = var.fencing_enabled == true && var.fencing_app_exists == true ? 1 : 0
display_name = var.fencing_app_name
}
resource "azuread_application" "azuread_app" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
display_name = var.fencing_app_name
owners = [data.azuread_client_config.current[0].object_id]
api {
oauth2_permission_scope {
admin_consent_description = "Allow the application to fence VMs on behalf of the signed-in user."
admin_consent_display_name = var.fencing_app_name
enabled = true
id = random_uuid.fencing_app_oauth2_permission_scope[0].result
type = "User"
user_consent_description = "Allow the application to fence VMs on your behalf."
user_consent_display_name = var.fencing_app_name
value = "user_impersonation"
}
}
app_role {
allowed_member_types = ["User", "Application"]
description = "Role to fence VMs"
display_name = format("%s_app_role", var.fencing_app_name)
value = format("%s_app_role", var.fencing_app_name)
id = random_uuid.fencing_app_app_role[0].result
enabled = true
}
}
data "azuread_service_principal" "azuread_service_principal" {
count = var.fencing_enabled == true && var.fencing_app_exists == true ? 1 : 0
application_id = data.azuread_application.azuread_app[0].application_id
}
resource "azuread_service_principal" "azuread_service_principal" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
application_id = azuread_application.azuread_app[0].application_id
app_role_assignment_required = false
}
resource "random_password" "azuread_rnd_password" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
length = 42
special = false
keepers = {
resource_group = local.resource_group
}
}
# Create random password for AzureAD Application
resource "azuread_application_password" "azuread_app_password" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
application_object_id = azuread_application.azuread_app[0].id
end_date = "2030-01-01T01:01:01Z"
}
# Create role definition for AzureAD Application
resource "azurerm_role_definition" "vm_role_definition" {
count = var.fencing_enabled == true && var.fencing_app_exists == false ? 1 : 0
name = format("%s_role_definition", var.fencing_app_name)
scope = (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].id
: azurerm_resource_group.resource_group[0].id
)
description = "This role allows AzureAD Application to fence VMs in Azure"
assignable_scopes = [(
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].id
: azurerm_resource_group.resource_group[0].id
)]
permissions {
actions = ["Microsoft.Compute/*/read",
"Microsoft.Compute/virtualMachines/powerOff/action",
"Microsoft.Compute/virtualMachines/start/action",
"Microsoft.Compute/virtualMachines/restart/action"]
not_actions = []
}
}
# Set permissions for the AzureAD Application in the VMs
resource "azurerm_role_assignment" "role_assign_vm_fencing_on_instance" {
for_each = {
for vm in var.vms
: vm.name => vm
if var.fencing_enabled == true && var.fencing_app_exists == false && var.fencing_perms_rg == false
}
# Scope based on virtual machines only
scope = azurerm_linux_virtual_machine.vm[each.key].id
role_definition_id = azurerm_role_definition.vm_role_definition[0].role_definition_resource_id
principal_id = (
var.fencing_app_exists == false
? azuread_service_principal.azuread_service_principal[0].object_id
: data.azuread_service_principal.azuread_service_principal[0].object_id
)
}
# Set permissions for the AzureAD Application in the resource group
resource "azurerm_role_assignment" "role_assign_vm_fencing_on_rg" {
for_each = {
for vm in var.vms
: vm.name => vm
if var.fencing_enabled == true && var.fencing_app_exists == false && var.fencing_perms_rg == true
}
# Scope based on resource group permission
scope = (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].id
: azurerm_resource_group.resource_group[0].id
)
role_definition_id = azurerm_role_definition.vm_role_definition[0].role_definition_resource_id
principal_id = (
var.fencing_app_exists == false
? azuread_service_principal.azuread_service_principal[0].object_id
: data.azuread_service_principal.azuread_service_principal[0].object_id
)
}
#######################
# Public IP addressses
#######################
resource "azurerm_public_ip" "public_ip" {
for_each = {
for vm in var.vms
: vm.name => vm
if vm.add_pub_ip == true
}
name = format("%s_public_ip", each.key)
location = local.location
resource_group_name = local.resource_group
allocation_method = (
each.value.pub_ip_persist == true
? "Static"
: "Dynamic"
)
sku = (
lower(var.lb_sku) != "basic"
? var.lb_sku
: "Basic"
)
tags = var.tags
}
############################
# Load Balancer for cluster
############################
# Create public IPs for Load Balancer
resource "azurerm_public_ip" "lb_public_ip" {
for_each = {
for frontend in var.lb_frontend_ip
: frontend.name => frontend
if var.lb_create == true && var.lb_create_public_ip == true
}
name = format("%s_public_ip", each.key)
location = local.location
resource_group_name = local.resource_group
allocation_method = (
lower(var.lb_sku) == "basic"
? "Dynamic"
: "Static"
)
sku = var.lb_sku
tags = var.tags
}
# Create load balancer
resource "azurerm_lb" "lb" {
count = var.lb_create == true ? 1 : 0
name = var.lb_name
location = local.location
resource_group_name = local.resource_group
sku = var.lb_sku
tags = var.tags
dynamic "frontend_ip_configuration" {
for_each = var.lb_frontend_ip
content {
name = format("%s_frontend_config", frontend_ip_configuration.value.name)
subnet_id = (
var.lb_create_public_ip == true
? null
: (
var.subnet_exists == true
? data.azurerm_subnet.subnet[0].id
: azurerm_subnet.subnet[0].id
)
)
private_ip_address_allocation = (
var.lb_create_public_ip == true
? null
: (
frontend_ip_configuration.value.private_ip == ""
? "Dynamic"
: "Static"
)
)
private_ip_address = (
var.lb_create_public_ip == true
? null
: (
frontend_ip_configuration.value.private_ip == ""
? null
: frontend_ip_configuration.value.private_ip
)
)
public_ip_address_id = (
var.lb_create_public_ip == true
? azurerm_public_ip.lb_public_ip[frontend_ip_configuration.value.name].id
: null
)
}
}
}
# Create load balancer backend address pool
resource "azurerm_lb_backend_address_pool" "lb_bap" {
count = var.lb_create == true ? 1 : 0
name = format("%s_backend_address_pool", var.lb_name)
loadbalancer_id = azurerm_lb.lb[0].id
}
resource "azurerm_network_interface_backend_address_pool_association" "lb_network_interface" {
for_each = {
for vm in var.vms
: vm.name => vm
if var.lb_create == true
}
ip_configuration_name = azurerm_network_interface.network_interface[each.key].ip_configuration[0].name
network_interface_id = azurerm_network_interface.network_interface[each.key].id
backend_address_pool_id = azurerm_lb_backend_address_pool.lb_bap[0].id
}
# Load balance probe rules
resource "azurerm_lb_probe" "lb_probe" {
for_each = {
for rule in var.lb_probe_rules
: rule.name => rule
if var.lb_create == true
}
name = format("lb_probe_%s", each.key)
loadbalancer_id = azurerm_lb.lb[0].id
protocol = each.value.protocol
request_path = (
lower(each.value.protocol) == "http" || lower(each.value.protocol) == "https"
? (
each.value.request_path == null
? "/"
: each.value.request_path
)
: null
)
port = each.value.port
interval_in_seconds = each.value.interval
number_of_probes = each.value.number_of_probes
}
# Load balance rules
resource "azurerm_lb_rule" "lb_rule" {
for_each = {
for rule in var.lb_rules
: rule.name => rule
if var.lb_create == true
}
name = format("lb_rule_%s", each.key)
loadbalancer_id = azurerm_lb.lb[0].id
frontend_ip_configuration_name = format("%s_frontend_config", each.value.frontend_name)
probe_id = azurerm_lb_probe.lb_probe[each.value.probe_name].id
backend_address_pool_ids = [azurerm_lb_backend_address_pool.lb_bap[0].id]
protocol = each.value.protocol
frontend_port = each.value.frontend_port
backend_port = each.value.backend_port
enable_floating_ip = each.value.enable_floating_ip
idle_timeout_in_minutes = each.value.idle_timeout_in_minutes
load_distribution = each.value.load_distribution
enable_tcp_reset = (
lower(var.lb_sku) == "standard"
? each.value.enable_tcp_reset
: false
)
disable_outbound_snat = each.value.disable_outbound_snat
}
# # Output rule for load balancer
# # Confliting when instance use public IP
# resource "azurerm_lb_outbound_rule" "lb_outbound_rule" {
# count = var.lb_create == true && var.lb_create_public_ip == true ? 1 : 0
# loadbalancer_id = azurerm_lb.lb[0].id
# name = "Outbound_All"
# protocol = "All"
# backend_address_pool_id = azurerm_lb_backend_address_pool.lb_bap[0].id
# resource_group_name = local.resource_group
#
# frontend_ip_configuration {
# name = azurerm_lb.lb[0].frontend_ip_configuration[0].name
# }
# }
#############################
# Load Balancer for outbound
#############################
# Create public IP for Load Balancer
resource "azurerm_public_ip" "lb_out_public_ip" {
count = var.lb_out_create == true ? 1 : 0
name = format("%s_public_ip", var.lb_out_name)
location = local.location
resource_group_name = local.resource_group
allocation_method = (
lower(var.lb_out_sku) == "basic"
? "Dynamic"
: "Static"
)
sku = var.lb_out_sku
tags = var.tags
}
# Create load balancer
resource "azurerm_lb" "lb_out" {
count = var.lb_out_create == true ? 1 : 0
name = var.lb_out_name
location = local.location
resource_group_name = local.resource_group
sku = var.lb_out_sku
tags = var.tags
frontend_ip_configuration {
name = format("%s_frontend_config", var.lb_out_name)
public_ip_address_id = azurerm_public_ip.lb_out_public_ip[0].id
}
}
# Create load balancer backend address pool
resource "azurerm_lb_backend_address_pool" "lb_out_bap" {
count = var.lb_out_create == true ? 1 : 0
name = format("%s_backend_address_pool", var.lb_out_name)
loadbalancer_id = azurerm_lb.lb_out[0].id
}
resource "azurerm_network_interface_backend_address_pool_association" "lb_out_network_interface" {
for_each = {
for vm in var.vms
: vm.name => vm
if var.lb_out_create == true && vm.add_pub_ip == false
}
ip_configuration_name = azurerm_network_interface.network_interface[each.key].ip_configuration[0].name
network_interface_id = azurerm_network_interface.network_interface[each.key].id
backend_address_pool_id = azurerm_lb_backend_address_pool.lb_out_bap[0].id
}
# Output rule for load balancer
resource "azurerm_lb_outbound_rule" "lb_out_rule" {
count = var.lb_out_create == true ? 1 : 0
loadbalancer_id = azurerm_lb.lb_out[0].id
name = "Outbound_All"
protocol = "All"
backend_address_pool_id = azurerm_lb_backend_address_pool.lb_out_bap[0].id
frontend_ip_configuration {
name = azurerm_lb.lb_out[0].frontend_ip_configuration[0].name
}
}
##############
# NAT Gateway
##############
data "azurerm_resource_group" "nat_gw_rg" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == true ? 1 : 0
name = var.nat_gateway_rg
}
resource "azurerm_public_ip" "nat_gw_pub_ip" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == false ? 1 : 0
name = format("%s_public_ip", var.nat_gateway_name)
location = local.location
resource_group_name = (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].name
: azurerm_resource_group.resource_group[0].name
)
allocation_method = (
var.nat_gateway_pub_ip_persist == true
? "Static"
: "Dynamic"
)
sku = (
lower(var.nat_gateway_sku) != "basic"
? var.nat_gateway_sku
: "Basic"
)
tags = var.tags
}
resource "azurerm_public_ip_prefix" "nat_gw_pub_ip_prefix" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == false ? 1 : 0
name = format("%s_public_ip_prefix", var.nat_gateway_name)
location = local.location
resource_group_name = (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].name
: azurerm_resource_group.resource_group[0].name
)
prefix_length = 31
tags = var.tags
}
data "azurerm_nat_gateway" "nat_gateway" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == true ? 1 : 0
name = var.nat_gateway_name
resource_group_name = data.azurerm_resource_group.nat_gw_rg[0].name
}
resource "azurerm_nat_gateway" "nat_gateway" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == false ? 1 : 0
name = var.nat_gateway_name
location = local.location
resource_group_name = (
var.rg_exists == true
? data.azurerm_resource_group.resource_group[0].name
: azurerm_resource_group.resource_group[0].name
)
sku_name = (
lower(var.nat_gateway_sku) != "basic"
? var.nat_gateway_sku
: "Basic"
)
idle_timeout_in_minutes = (
var.nat_gateway_idle_timeout == 0
? 5
: var.nat_gateway_idle_timeout
)
tags = var.tags
}
resource "azurerm_nat_gateway_public_ip_association" "nat_gw_pub_ip_assoc" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == false ? 1 : 0
nat_gateway_id = (
var.nat_gateway_exists == true
? data.azurerm_nat_gateway.nat_gateway[0].id
: azurerm_nat_gateway.nat_gateway[0].id
)
public_ip_address_id = (
var.nat_gateway_exists == true
? data.azurerm_nat_gateway.nat_gateway[0].public_ip_address_ids[0]
: azurerm_public_ip.nat_gw_pub_ip[0].id
)
}
resource "azurerm_nat_gateway_public_ip_prefix_association" "nat_gw_pub_ip_prefix_assoc" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == false ? 1 : 0
nat_gateway_id = (
var.nat_gateway_exists == true
? data.azurerm_nat_gateway.nat_gateway[0].id
: azurerm_nat_gateway.nat_gateway[0].id
)
public_ip_prefix_id = (
var.nat_gateway_exists == true
? data.azurerm_nat_gateway.nat_gateway[0].public_ip_prefix_ids[0]
: azurerm_public_ip_prefix.nat_gw_pub_ip_prefix[0].id
)
}
resource "azurerm_subnet_nat_gateway_association" "nat_gw_subnet_assoc" {
count = var.nat_gateway_enabled == true && var.nat_gateway_exists == false ? 1 : 0
subnet_id = (
var.subnet_exists == true
? data.azurerm_subnet.subnet[0].id
: azurerm_subnet.subnet[0].id
)
nat_gateway_id = (
var.nat_gateway_exists == true
? data.azurerm_nat_gateway.nat_gateway[0].id
: azurerm_nat_gateway.nat_gateway[0].id
)
}
#########################
# Network security group
#########################
data "azurerm_resource_group" "nsg" {
count = var.nsg_enabled == true && var.nsg_exists == true ? 1 : 0
name = var.nsg_rg
}
data "azurerm_network_security_group" "nsg" {
count = var.nsg_enabled == true && var.nsg_exists == true ? 1 : 0
name = var.nsg_name
resource_group_name = data.azurerm_resource_group.nsg[0].name
}
resource "azurerm_network_security_group" "nsg" {
count = var.nsg_enabled == true && var.nsg_exists == false ? 1 : 0
name = var.nsg_name
location = local.location
resource_group_name = (
var.nsg_rg == ""
? local.resource_group
: var.nsg_rg
)
tags = var.tags
}
# Create Network Security rules
resource "azurerm_network_security_rule" "nsg_rule" {
for_each = {
for nsg_rule in var.nsg_rules
: nsg_rule.name => nsg_rule
if var.nsg_enabled == true && nsg_rule.enabled == true
}
name = each.key
priority = local.nsg_priority_base + index(var.nsg_rules.*.name, each.key) + 1
direction = each.value.direction
access = each.value.access
protocol = each.value.protocol
source_port_range = each.value.source_port_range
destination_port_range = each.value.destination_port_range
source_address_prefix = each.value.source_address_prefix
destination_address_prefix = each.value.destination_address_prefix
resource_group_name = (
var.nsg_exists == true
? data.azurerm_resource_group.nsg[0].name
: (
var.nsg_rg == ""
? local.resource_group
: var.nsg_rg
)
)
network_security_group_name = (
var.nsg_exists == true
? data.azurerm_network_security_group.nsg[0].name
: azurerm_network_security_group.nsg[0].name
)
}
#####################
# Network interfaces
#####################
resource "azurerm_network_interface" "network_interface" {
for_each = {
for vm in var.vms
: vm.name => vm
}
name = format("%s_nic_%s", each.key, 1)
location = local.location
resource_group_name = local.resource_group
enable_accelerated_networking = each.value.net_accel
tags = var.tags
ip_configuration {
name = format("%s_nic_config", each.key)
subnet_id = local.subnet_id
private_ip_address_allocation = (
each.value.priv_ip_dynamic == true
? "Dynamic"
: "Static"
)
private_ip_address = (
each.value.priv_ip_dynamic == true
? null
: each.value.priv_ip_addr
)
public_ip_address_id = (
each.value.add_pub_ip == true
? azurerm_public_ip.public_ip[each.key].id
: null
)
}
}
resource "azurerm_network_interface_security_group_association" "network_nsg_association" {
for_each = {
for vm in var.vms
: vm.name => vm
if var.nsg_enabled == true
}
network_interface_id = azurerm_network_interface.network_interface[each.key].id
network_security_group_id = (
var.nsg_exists == true
? data.azurerm_network_security_group.nsg[0].id
: azurerm_network_security_group.nsg[0].id
)
}
################################
# Storage account for SAP media
################################
data "azurerm_resource_group" "rg_sap_media" {
count = var.sap_media_stg_enabled == true ? 1 : 0
name = var.sap_media_stg_rg
}
data "azurerm_storage_account" "stg_sap_media" {
count = var.sap_media_stg_enabled == true && var.sap_media_stg_exists == true ? 1 : 0
name = var.sap_media_stg_account
resource_group_name = data.azurerm_resource_group.rg_sap_media[0].name
}
data "http" "stg_sap_media_get_pub_ip" {
count = var.sap_media_stg_enabled == true && var.sap_media_stg_exists == false ? 1 : 0
url = "https://ifconfig.me/ip"
}
resource "azurerm_storage_account" "stg_sap_media" {
count = var.sap_media_stg_enabled == true && var.sap_media_stg_exists == false ? 1 : 0
name = var.sap_media_stg_account
resource_group_name = data.azurerm_resource_group.rg_sap_media[0].name
location = data.azurerm_resource_group.rg_sap_media[0].location
account_tier = var.sap_media_stg_tier
account_replication_type = var.sap_media_stg_repl
account_kind = var.sap_media_stg_account_kind
enable_https_traffic_only = (
var.sap_media_stg_type == "nfsv4"
? false
: true
)
is_hns_enabled = (
var.sap_media_stg_type == "nfsv3"
? true
: false
)
nfsv3_enabled = (
var.sap_media_stg_type == "nfsv3"
? true
: false
)
tags = var.tags
}
# Adding network_rules in a existing storage account is not supported
# https://github.com/hashicorp/terraform-provider-azurerm/issues/13640
resource "azurerm_storage_account_network_rules" "stg_sap_media_network_rules" {
count = var.sap_media_stg_enabled == true && var.sap_media_stg_exists == false ? 1 : 0
storage_account_id = azurerm_storage_account.stg_sap_media[0].id
default_action = "Deny"
bypass = ["AzureServices"]
ip_rules = [chomp(data.http.stg_sap_media_get_pub_ip[0])]
virtual_network_subnet_ids = [(
var.subnet_exists == true
? data.azurerm_subnet.subnet[0].id
: azurerm_subnet.subnet[0].id
)]
}
# Use a existing private endpoint for SAP Media
data "azurerm_private_endpoint_connection" "pe_stg_sap_media" {
count = var.sap_media_stg_enabled == true && var.sap_media_stg_pe_enabled == true && var.sap_media_stg_pe_exists == true ? 1 : 0
name = (
var.sap_media_stg_pe_name == ""
? format("pe_stg_sap_media_%s", var.sap_media_stg_account)
: var.sap_media_stg_pe_name
)
resource_group_name = data.azurerm_resource_group.rg_sap_media[0].name
}
# Create private endpoint for SAP Media network access to NFSv4
resource "azurerm_private_endpoint" "pe_stg_sap_media" {
count = var.sap_media_stg_enabled == true && var.sap_media_stg_pe_enabled == true && var.sap_media_stg_pe_exists == false ? 1 : 0
name = (
var.sap_media_stg_pe_name == ""
? format("pe_stg_sap_media_%s", var.sap_media_stg_account)
: var.sap_media_stg_pe_name
)
resource_group_name = data.azurerm_resource_group.rg_sap_media[0].name
location = data.azurerm_resource_group.rg_sap_media[0].location
subnet_id = (
var.subnet_exists == true
? data.azurerm_subnet.subnet[0].id
: azurerm_subnet.subnet[0].id
)
tags = var.tags
private_service_connection {
name = (