-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemplate-latest.tf
597 lines (497 loc) · 19.6 KB
/
template-latest.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
terraform {
required_version = ">=0.12"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
azapi = {
source = "azure/azapi"
version = "=1.4.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider "azapi" {
default_location = var.resource_group_location
}
provider "azurerm" {
features {}
}
locals {
storage_account_name = "storage${var.suffix}"
total_disk_count = var.sql_log_disk_count + var.sql_data_disk_count
data_disks_luns = [for i in range(0, var.sql_data_disk_count) : i]
log_disks_luns = [for i in range(var.sql_data_disk_count, var.sql_data_disk_count + var.sql_log_disk_count) : i]
virtual_network_name = "vnet-${var.suffix}"
network_interface_name = "${var.virtual_machine_name}-nic"
network_security_group_name = "${var.virtual_machine_name}-nsg"
public_ip_address_name = "${var.virtual_machine_name}-publicip-${random_id.random_deployment_suffix.hex}"
sql_mi_network_security_group_name = "sqlmi-${var.suffix}-nsg"
sql_mi_route_table_name = "sqlmi-${var.suffix}-route-table"
sql_mi_name = "sqlmi-${var.suffix}"
}
resource "random_id" "random_deployment_suffix" {
byte_length = 4
}
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = var.resource_group_name
}
resource "azurerm_storage_account" "sta" {
name = local.storage_account_name
resource_group_name = azurerm_resource_group.rg.name
location = var.resource_group_location
account_tier = "Standard"
account_replication_type = "LRS"
allow_blob_public_access = true
}
resource "azurerm_storage_container" "container" {
name = "migration"
storage_account_name = azurerm_storage_account.sta.name
container_access_type = "blob"
}
resource "azapi_resource" "dms" {
type = "Microsoft.DataMigration/sqlMigrationServices@2022-03-30-preview"
name = var.migration_service_name
location = azurerm_resource_group.rg.location
parent_id = azurerm_resource_group.rg.id
}
resource "azurerm_public_ip" "publicip" {
name = local.public_ip_address_name
resource_group_name = azurerm_resource_group.rg.name
location = var.resource_group_location
allocation_method = var.public_ip_address_type
sku = var.public_ip_address_sku
}
resource "azurerm_network_security_group" "nsg" {
name = local.network_security_group_name
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_virtual_network" "vnet" {
name = local.virtual_network_name
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
address_space = [var.vnet_address_prefix]
subnet {
name = var.subnet1_name
address_prefix = var.subnet1_prefix
security_group = azurerm_network_security_group.nsg.id
}
}
resource "azurerm_network_interface" "network_interface" {
name = local.network_interface_name
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "ipconfig1"
subnet_id = "${azurerm_virtual_network.vnet.id}/subnets/${var.subnet1_name}"
private_ip_address_allocation = "Static"
public_ip_address_id = azurerm_public_ip.publicip.id
private_ip_address = "10.1.0.4"
}
}
resource "azurerm_managed_disk" "data_disk" {
count = local.total_disk_count
name = "${var.sql_vm_name}-disk-${count.index}"
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = 1023
}
resource "azurerm_virtual_machine_data_disk_attachment" "data_disk_attachment" {
count = local.total_disk_count
managed_disk_id = azurerm_managed_disk.data_disk.*.id[count.index]
virtual_machine_id = azurerm_virtual_machine.vm.id
lun = count.index
caching = "ReadWrite"
}
resource "azurerm_virtual_machine" "vm" {
name = var.sql_vm_name
resource_group_name = azurerm_resource_group.rg.name
location = var.resource_group_location
vm_size = var.virtual_machine_size
network_interface_ids = [azurerm_network_interface.network_interface.id]
os_profile {
computer_name = var.sql_vm_name
admin_username = var.admin_username
admin_password = var.admin_password
}
os_profile_windows_config {
provision_vm_agent = true
enable_automatic_upgrades = true
}
storage_os_disk {
name = "osdisk001"
managed_disk_type = "Premium_LRS"
create_option = "FromImage"
}
storage_image_reference {
publisher = "MicrosoftSQLServer"
offer = var.image_offer
sku = var.sql_sku
version = "latest"
}
}
resource "azurerm_network_security_group" "sql_mi_nsg" {
name = local.sql_mi_network_security_group_name
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_network_security_rule" "sql_mi_allow_tds_inbound" {
name = "allow_tds_inbound"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "1433"
source_address_prefix = "*"
destination_address_prefix = "*"
access = "Allow"
priority = 1000
direction = "Inbound"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.sql_mi_nsg.name
}
resource "azurerm_network_security_rule" "sql_mi_allow_redirect_inbound" {
name = "allow_redirect_inbound"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "11000-11999"
source_address_prefix = "VirtualNetwork"
destination_address_prefix = "*"
access = "Allow"
priority = 1100
direction = "Inbound"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.sql_mi_nsg.name
}
resource "azurerm_network_security_rule" "sql_mi_public_endpoint_inbound" {
name = "public_endpoint_inbound"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "3342"
source_address_prefix = "*"
destination_address_prefix = "*"
access = "Allow"
priority = 1300
direction = "Inbound"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.sql_mi_nsg.name
}
resource "azurerm_network_security_rule" "sql_mi_deny_all_inbound" {
name = "deny_all_inbound"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
access = "Deny"
priority = 4096
direction = "Inbound"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.sql_mi_nsg.name
}
resource "azurerm_network_security_rule" "sql_mi_deny_all_outbound" {
name = "deny_all_outbound"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
access = "Deny"
priority = 4096
direction = "Outbound"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.sql_mi_nsg.name
}
resource "azurerm_route_table" "sql_mi_route_table" {
name = local.sql_mi_route_table_name
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
disable_bgp_route_propagation = false
}
resource "azurerm_virtual_network" "sql_mi_vnet" {
name = var.sql_mi_virtualnetworkname
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
address_space = [var.sql_mi_address_prefix]
}
resource "azurerm_subnet" "sql_mi_subnet" {
name = var.sql_mi_subnetname
address_prefixes = [var.sql_mi_subnet_prefix]
virtual_network_name = azurerm_virtual_network.sql_mi_vnet.name
resource_group_name = azurerm_resource_group.rg.name
delegation {
name = "delegation"
service_delegation {
name = "Microsoft.Sql/managedInstances"
#actions = ["Microsoft.Network/virtualNetworks/subnets/join/action", "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action"]
}
}
}
resource "azurerm_subnet_route_table_association" "sql_mi_subnet_route_association" {
subnet_id = azurerm_subnet.sql_mi_subnet.id
route_table_id = azurerm_route_table.sql_mi_route_table.id
}
resource "azurerm_subnet_network_security_group_association" "subnet_sql_mi_association" {
subnet_id = azurerm_subnet.sql_mi_subnet.id
network_security_group_id = azurerm_network_security_group.sql_mi_nsg.id
}
resource "azurerm_subnet" "sql_mi_management_subnet" {
name = var.sql_mi_management_subnet_name
address_prefixes = [var.sql_mi_management_subnet_prefix]
virtual_network_name = azurerm_virtual_network.sql_mi_vnet.name
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet_network_security_group_association" "subnet_sql_mi_management_association" {
subnet_id = azurerm_subnet.sql_mi_management_subnet.id
network_security_group_id = azurerm_network_security_group.sql_mi_nsg.id
}
resource "azurerm_sql_managed_instance" "sql_mi" {
name = local.sql_mi_name
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
sku_name = var.sql_mi_sku_name
administrator_login = var.admin_username
administrator_login_password = var.sql_administrator_login_password
vcores = var.sql_mi_vcores
license_type = var.sql_mi_license_type
subnet_id = azurerm_subnet.sql_mi_subnet.id
storage_size_in_gb = var.sql_mi_storage_size_in_gb
public_data_endpoint_enabled = true
minimum_tls_version = "1.2"
identity {
type = "SystemAssigned"
}
timeouts {
create = "60m"
update = "60m"
delete = "60m"
read = "60m"
}
depends_on = [
azurerm_virtual_network.sql_mi_vnet
]
}
resource "azurerm_mssql_virtual_machine" "sql_vm" {
virtual_machine_id = azurerm_virtual_machine.vm.id
sql_license_type = "PAYG"
sql_connectivity_port = 1433
sql_connectivity_type = "PUBLIC"
sql_connectivity_update_password = var.admin_password
sql_connectivity_update_username = var.admin_username
storage_configuration {
disk_type = "NEW"
storage_workload_type = "GENERAL"
data_settings {
luns = local.data_disks_luns
default_file_path = var.data_path
}
log_settings {
luns = local.log_disks_luns
default_file_path = var.log_path
}
}
depends_on = [azurerm_virtual_machine_data_disk_attachment.data_disk_attachment]
}
resource "azurerm_virtual_machine_extension" "vm_extension" {
name = "CustomScriptExtension"
virtual_machine_id = azurerm_virtual_machine.vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
settings = <<SETTINGS
{
"fileUris": ["https://raw.githubusercontent.com/Azure/SQL-Migration-AzureSQL-PoC/main/script/SQLVMPostInstallation.ps1"],
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File SQLVMPostInstallation.ps1"
}
SETTINGS
timeouts {
create = "60m"
update = "60m"
delete = "60m"
read = "60m"
}
depends_on = [
azurerm_mssql_virtual_machine.sql_vm
]
}
resource "azurerm_virtual_machine_extension" "jb_vm_extension" {
name = "CustomScriptExtension"
virtual_machine_id = azurerm_windows_virtual_machine.jb_vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
settings = <<SETTINGS
{
"fileUris": ["https://raw.githubusercontent.com/Azure/SQL-Migration-AzureSQL-PoC/main/script/JumpBoxPostInstallation.ps1"],
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File JumpBoxPostInstallation.ps1"
}
SETTINGS
timeouts {
create = "60m"
update = "60m"
delete = "60m"
read = "60m"
}
}
# resource "azurerm_mssql_server" "sqlserver" {
# name = "${var.sql_server_name}-${var.suffix}"
# resource_group_name = azurerm_resource_group.rg.name
# location = var.resource_group_location
# version = "12.0"
# administrator_login = var.sql_administrator_login
# administrator_login_password = var.sql_administrator_login_password
# tags = {
# displayName = "${var.sql_server_name}-${var.suffix}"
# }
# }
# resource "azurerm_mssql_database" "mssql_database" {
# name = var.database_name
# server_id = azurerm_mssql_server.sqlserver.id
# collation = "SQL_Latin1_General_CP1_CI_AS"
# max_size_gb = 50
# read_scale = false
# sku_name = "S2"
# tags = {
# displayName = var.database_name
# }
# }
resource "azurerm_network_security_rule" "rdp_rule" {
name = "RDP"
priority = 300
direction = "Inbound"
access = "Allow"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}
resource "azurerm_network_security_rule" "allow_any_ms_sql_inbound_rule" {
name = "AllowAnyMS_SQLInbound"
priority = 310
direction = "Inbound"
access = "Allow"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "1433"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}
resource "azurerm_network_security_group" "jb_nsg" {
name = var.jb_nsg_name
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_network_security_rule" "rdp_jb_rule" {
name = "RDP"
priority = 300
direction = "Inbound"
access = "Allow"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.jb_nsg.name
}
resource "azurerm_virtual_network" "jb_vnet" {
name = var.jb_vnet_name
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
address_space = ["10.2.0.0/16"]
subnet {
name = "default"
address_prefix = "10.2.0.0/24"
security_group = azurerm_network_security_group.jb_nsg.id
}
}
resource "azurerm_public_ip" "jp_publicip" {
name = var.jb_ip_address_name
resource_group_name = azurerm_resource_group.rg.name
location = var.resource_group_location
allocation_method = "Dynamic"
sku = "Basic"
}
resource "azurerm_network_interface" "jb_nic" {
name = "${var.jb_interface_name}-nic"
location = var.resource_group_location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "ipconfig1"
subnet_id = "${azurerm_virtual_network.jb_vnet.id}/subnets/default"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.jp_publicip.id
}
}
resource "azurerm_windows_virtual_machine" "jb_vm" {
name = var.jb_vm_name
resource_group_name = azurerm_resource_group.rg.name
location = var.resource_group_location
size = "Standard_B4ms"
admin_username = var.admin_username
admin_password = var.admin_password
network_interface_ids = [
azurerm_network_interface.jb_nic.id,
]
os_disk {
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsDesktop"
offer = "Windows-11"
sku = "win11-22h2-pro"
version = "latest"
}
}
resource "azurerm_virtual_network_peering" "peering_to_vnet" {
name = "peeringTo${azurerm_virtual_network.vnet.name}"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.jb_vnet.name
remote_virtual_network_id = azurerm_virtual_network.vnet.id
allow_virtual_network_access = true
allow_forwarded_traffic = false
allow_gateway_transit = false
use_remote_gateways = false
}
resource "azurerm_virtual_network_peering" "peering_to_sql_mi_vnet" {
name = "peeringTo${azurerm_virtual_network.sql_mi_vnet.name}"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.jb_vnet.name
remote_virtual_network_id = azurerm_virtual_network.sql_mi_vnet.id
allow_virtual_network_access = true
allow_forwarded_traffic = false
allow_gateway_transit = false
use_remote_gateways = false
}
resource "azurerm_virtual_network_peering" "peering_from_sql_mi_vnet_to_jb_vnet" {
name = "peeringFrom${azurerm_virtual_network.sql_mi_vnet.name}To${azurerm_virtual_network.jb_vnet.name}"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.sql_mi_vnet.name
remote_virtual_network_id = azurerm_virtual_network.jb_vnet.id
allow_virtual_network_access = true
allow_forwarded_traffic = false
allow_gateway_transit = false
use_remote_gateways = false
}
resource "azurerm_virtual_network_peering" "peering_to_jb_vnet" {
name = "peeringTo${azurerm_virtual_network.jb_vnet.name}"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
remote_virtual_network_id = azurerm_virtual_network.jb_vnet.id
allow_virtual_network_access = true
allow_forwarded_traffic = false
allow_gateway_transit = false
use_remote_gateways = false
}