-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.tf
440 lines (379 loc) · 21.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
locals {
resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0)
location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0)
if_threat_detection_policy_enabled = var.enable_threat_detection_policy ? [{}] : []
#if_extended_auditing_policy_enabled = var.enable_extended_auditing_policy ? [{}] : []
}
#---------------------------------------------------------
# Resource Group Creation or selection - Default is "false"
#----------------------------------------------------------
data "azurerm_resource_group" "rgrp" {
count = var.create_resource_group == false ? 1 : 0
name = var.resource_group_name
}
resource "azurerm_resource_group" "rg" {
count = var.create_resource_group ? 1 : 0
name = var.resource_group_name
location = var.location
tags = merge({ "Name" = format("%s", var.resource_group_name) }, var.tags, )
}
data "azurerm_client_config" "current" {}
#---------------------------------------------------------
# Storage Account to keep Audit logs - Default is "false"
#----------------------------------------------------------
resource "random_string" "str" {
count = var.enable_sql_server_extended_auditing_policy || var.enable_database_extended_auditing_policy || var.enable_vulnerability_assessment ? 1 : 0
length = 6
special = false
upper = false
keepers = {
name = var.storage_account_name
}
}
resource "azurerm_storage_account" "storeacc" {
count = var.enable_sql_server_extended_auditing_policy || var.enable_database_extended_auditing_policy || var.enable_vulnerability_assessment || var.enable_log_monitoring == true ? 1 : 0
name = var.storage_account_name == null ? "stsqlauditlogs${element(concat(random_string.str.*.result, [""]), 0)}" : substr(var.storage_account_name, 0, 24)
resource_group_name = local.resource_group_name
location = local.location
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "GRS"
enable_https_traffic_only = true
min_tls_version = "TLS1_2"
tags = merge({ "Name" = format("%s", "stsqlauditlogs") }, var.tags, )
}
resource "azurerm_storage_container" "storcont" {
count = var.enable_vulnerability_assessment ? 1 : 0
name = "vulnerability-assessment"
storage_account_name = azurerm_storage_account.storeacc.0.name
container_access_type = "private"
}
#-------------------------------------------------------------
# SQL servers - Secondary server is depends_on Failover Group
#-------------------------------------------------------------
resource "random_password" "main" {
length = var.random_password_length
min_upper = 4
min_lower = 2
min_numeric = 4
special = false
keepers = {
administrator_login_password = var.sqlserver_name
}
}
resource "azurerm_sql_server" "primary" {
name = format("%s-primary", var.sqlserver_name)
resource_group_name = local.resource_group_name
location = local.location
version = "12.0"
administrator_login = var.admin_username == null ? "sqladmin" : var.admin_username
administrator_login_password = var.admin_password == null ? random_password.main.result : var.admin_password
tags = merge({ "Name" = format("%s-primary", var.sqlserver_name) }, var.tags, )
dynamic "identity" {
for_each = var.identity == true ? [1] : [0]
content {
type = "SystemAssigned"
}
}
}
resource "azurerm_mssql_server_extended_auditing_policy" "primary" {
count = var.enable_sql_server_extended_auditing_policy ? 1 : 0
server_id = azurerm_sql_server.primary.id
storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
storage_account_access_key_is_secondary = false
retention_in_days = var.log_retention_days
log_monitoring_enabled = var.enable_log_monitoring == true && var.log_analytics_workspace_id != null ? true : false
}
resource "azurerm_sql_server" "secondary" {
count = var.enable_failover_group ? 1 : 0
name = format("%s-secondary", var.sqlserver_name)
resource_group_name = local.resource_group_name
location = var.secondary_sql_server_location
version = "12.0"
administrator_login = var.admin_username == null ? "sqladmin" : var.admin_username
administrator_login_password = var.admin_password == null ? random_password.main.result : var.admin_password
tags = merge({ "Name" = format("%s-secondary", var.sqlserver_name) }, var.tags, )
dynamic "identity" {
for_each = var.identity == true ? [1] : [0]
content {
type = "SystemAssigned"
}
}
}
resource "azurerm_mssql_server_extended_auditing_policy" "secondary" {
count = var.enable_failover_group && var.enable_sql_server_extended_auditing_policy ? 1 : 0
server_id = azurerm_sql_server.secondary.0.id
storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
storage_account_access_key_is_secondary = false
retention_in_days = var.log_retention_days
log_monitoring_enabled = var.enable_log_monitoring == true && var.log_analytics_workspace_id != null ? true : null
}
#--------------------------------------------------------------------
# SQL Database creation - Default edition:"Standard" and objective:"S1"
#--------------------------------------------------------------------
resource "azurerm_sql_database" "db" {
name = var.database_name
resource_group_name = local.resource_group_name
location = local.location
server_name = azurerm_sql_server.primary.name
edition = var.sql_database_edition
requested_service_objective_name = var.sqldb_service_objective_name
tags = merge({ "Name" = format("%s-primary", var.database_name) }, var.tags, )
dynamic "threat_detection_policy" {
for_each = local.if_threat_detection_policy_enabled
content {
state = "Enabled"
storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
retention_days = var.log_retention_days
email_addresses = var.email_addresses_for_alerts
}
}
}
resource "azurerm_mssql_database_extended_auditing_policy" "primary" {
count = var.enable_database_extended_auditing_policy ? 1 : 0
database_id = azurerm_sql_database.db.id
storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
storage_account_access_key_is_secondary = false
retention_in_days = var.log_retention_days
log_monitoring_enabled = var.enable_log_monitoring == true && var.log_analytics_workspace_id != null ? true : null
}
#-----------------------------------------------------------------------------------------------
# SQL ServerVulnerability assessment and alert to admin team - Default is "false"
#-----------------------------------------------------------------------------------------------
resource "azurerm_mssql_server_security_alert_policy" "sap_primary" {
count = var.enable_vulnerability_assessment ? 1 : 0
resource_group_name = local.resource_group_name
server_name = azurerm_sql_server.primary.name
state = "Enabled"
email_account_admins = true
email_addresses = var.email_addresses_for_alerts
retention_days = var.threat_detection_audit_logs_retention_days
disabled_alerts = var.disabled_alerts
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint
}
resource "azurerm_mssql_server_security_alert_policy" "sap_secondary" {
count = var.enable_vulnerability_assessment && var.enable_failover_group ? 1 : 0
resource_group_name = local.resource_group_name
server_name = azurerm_sql_server.secondary.0.name
state = "Enabled"
email_account_admins = true
email_addresses = var.email_addresses_for_alerts
retention_days = var.threat_detection_audit_logs_retention_days
disabled_alerts = var.disabled_alerts
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint
}
resource "azurerm_mssql_server_vulnerability_assessment" "va_primary" {
count = var.enable_vulnerability_assessment ? 1 : 0
server_security_alert_policy_id = azurerm_mssql_server_security_alert_policy.sap_primary.0.id
storage_container_path = "${azurerm_storage_account.storeacc.0.primary_blob_endpoint}${azurerm_storage_container.storcont.0.name}/"
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
recurring_scans {
enabled = true
email_subscription_admins = true
emails = var.email_addresses_for_alerts
}
}
resource "azurerm_mssql_server_vulnerability_assessment" "va_secondary" {
count = var.enable_vulnerability_assessment && var.enable_failover_group == true ? 1 : 0
server_security_alert_policy_id = azurerm_mssql_server_security_alert_policy.sap_secondary.0.id
storage_container_path = "${azurerm_storage_account.storeacc.0.primary_blob_endpoint}${azurerm_storage_container.storcont.0.name}/"
storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
recurring_scans {
enabled = true
email_subscription_admins = true
emails = var.email_addresses_for_alerts
}
}
#-----------------------------------------------------------------------------------------------
# Create and initialize a Microsoft SQL Server database using sqlcmd utility - Default is "false"
#-----------------------------------------------------------------------------------------------
resource "null_resource" "create_sql" {
count = var.initialize_sql_script_execution ? 1 : 0
provisioner "local-exec" {
command = "sqlcmd -I -U ${azurerm_sql_server.primary.administrator_login} -P ${azurerm_sql_server.primary.administrator_login_password} -S ${azurerm_sql_server.primary.fully_qualified_domain_name} -d ${azurerm_sql_database.db.name} -i ${var.sqldb_init_script_file} -o ${format("%s.log", replace(var.sqldb_init_script_file, "/.sql/", ""))}"
}
}
#-----------------------------------------------------------------------------------------------
# Adding AD Admin to SQL Server - Secondary server depend on Failover Group - Default is "false"
#-----------------------------------------------------------------------------------------------
resource "azurerm_sql_active_directory_administrator" "aduser1" {
count = var.ad_admin_login_name != null ? 1 : 0
server_name = azurerm_sql_server.primary.name
resource_group_name = local.resource_group_name
login = var.ad_admin_login_name
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
}
resource "azurerm_sql_active_directory_administrator" "aduser2" {
count = var.enable_failover_group && var.ad_admin_login_name != null ? 1 : 0
server_name = azurerm_sql_server.secondary.0.name
resource_group_name = local.resource_group_name
login = var.ad_admin_login_name
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
}
#---------------------------------------------------------
# Azure SQL Firewall Rule - Default is "false"
#---------------------------------------------------------
resource "azurerm_sql_firewall_rule" "fw01" {
count = var.enable_firewall_rules && length(var.firewall_rules) > 0 ? length(var.firewall_rules) : 0
name = element(var.firewall_rules, count.index).name
resource_group_name = local.resource_group_name
server_name = azurerm_sql_server.primary.name
start_ip_address = element(var.firewall_rules, count.index).start_ip_address
end_ip_address = element(var.firewall_rules, count.index).end_ip_address
}
resource "azurerm_sql_firewall_rule" "fw02" {
count = var.enable_failover_group && var.enable_firewall_rules && length(var.firewall_rules) > 0 ? length(var.firewall_rules) : 0
name = element(var.firewall_rules, count.index).name
resource_group_name = local.resource_group_name
server_name = azurerm_sql_server.secondary.0.name
start_ip_address = element(var.firewall_rules, count.index).start_ip_address
end_ip_address = element(var.firewall_rules, count.index).end_ip_address
}
#---------------------------------------------------------
# Azure SQL Failover Group - Default is "false"
#---------------------------------------------------------
resource "azurerm_sql_failover_group" "fog" {
count = var.enable_failover_group ? 1 : 0
name = "sqldb-failover-group"
resource_group_name = local.resource_group_name
server_name = azurerm_sql_server.primary.name
databases = [azurerm_sql_database.db.id]
tags = merge({ "Name" = format("%s", "sqldb-failover-group") }, var.tags, )
partner_servers {
id = azurerm_sql_server.secondary.0.id
}
read_write_endpoint_failover_policy {
mode = "Automatic"
grace_minutes = 60
}
readonly_endpoint_failover_policy {
mode = "Enabled"
}
}
#---------------------------------------------------------
# Private Link for SQL Server - Default is "false"
#---------------------------------------------------------
data "azurerm_virtual_network" "vnet01" {
count = var.enable_private_endpoint && var.existing_vnet_id == null ? 1 : 0
name = var.virtual_network_name
resource_group_name = local.resource_group_name
}
resource "azurerm_subnet" "snet-ep" {
count = var.enable_private_endpoint && var.existing_subnet_id == null ? 1 : 0
name = "snet-endpoint-${local.location}"
resource_group_name = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.resource_group_name : element(split("/", var.existing_vnet_id), 4)
virtual_network_name = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.name : element(split("/", var.existing_vnet_id), 8)
address_prefixes = var.private_subnet_address_prefix
enforce_private_link_endpoint_network_policies = true
}
resource "azurerm_private_endpoint" "pep1" {
count = var.enable_private_endpoint ? 1 : 0
name = format("%s-primary", "sqldb-private-endpoint")
location = local.location
resource_group_name = local.resource_group_name
subnet_id = var.existing_subnet_id == null ? azurerm_subnet.snet-ep.0.id : var.existing_subnet_id
tags = merge({ "Name" = format("%s", "sqldb-private-endpoint") }, var.tags, )
private_service_connection {
name = "sqldbprivatelink-primary"
is_manual_connection = false
private_connection_resource_id = azurerm_sql_server.primary.id
subresource_names = ["sqlServer"]
}
}
resource "azurerm_private_endpoint" "pep2" {
count = var.enable_failover_group && var.enable_private_endpoint ? 1 : 0
name = format("%s-secondary", "sqldb-private-endpoint")
location = local.location
resource_group_name = local.resource_group_name
subnet_id = var.existing_subnet_id == null ? azurerm_subnet.snet-ep.0.id : var.existing_subnet_id
tags = merge({ "Name" = format("%s", "sqldb-private-endpoint") }, var.tags, )
private_service_connection {
name = "sqldbprivatelink-secondary"
is_manual_connection = false
private_connection_resource_id = azurerm_sql_server.secondary.0.id
subresource_names = ["sqlServer"]
}
}
#------------------------------------------------------------------
# DNS zone & records for SQL Private endpoints - Default is "false"
#------------------------------------------------------------------
data "azurerm_private_endpoint_connection" "private-ip1" {
count = var.enable_private_endpoint ? 1 : 0
name = azurerm_private_endpoint.pep1.0.name
resource_group_name = local.resource_group_name
depends_on = [azurerm_sql_server.primary]
}
data "azurerm_private_endpoint_connection" "private-ip2" {
count = var.enable_failover_group && var.enable_private_endpoint ? 1 : 0
name = azurerm_private_endpoint.pep2.0.name
resource_group_name = local.resource_group_name
depends_on = [azurerm_sql_server.secondary]
}
resource "azurerm_private_dns_zone" "dnszone1" {
count = var.existing_private_dns_zone == null && var.enable_private_endpoint ? 1 : 0
name = "privatelink.database.windows.net"
resource_group_name = local.resource_group_name
tags = merge({ "Name" = format("%s", "SQL-Private-DNS-Zone") }, var.tags, )
}
resource "azurerm_private_dns_zone_virtual_network_link" "vent-link1" {
count = var.enable_private_endpoint ? 1 : 0
name = "vnet-private-zone-link"
resource_group_name = local.resource_group_name
private_dns_zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
virtual_network_id = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.id : var.existing_vnet_id
registration_enabled = true
tags = merge({ "Name" = format("%s", "vnet-private-zone-link") }, var.tags, )
}
resource "azurerm_private_dns_a_record" "arecord1" {
count = var.enable_private_endpoint ? 1 : 0
name = azurerm_sql_server.primary.name
zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
resource_group_name = local.resource_group_name
ttl = 300
records = [data.azurerm_private_endpoint_connection.private-ip1.0.private_service_connection.0.private_ip_address]
}
resource "azurerm_private_dns_a_record" "arecord2" {
count = var.enable_failover_group && var.enable_private_endpoint ? 1 : 0
name = azurerm_sql_server.secondary.0.name
zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
resource_group_name = local.resource_group_name
ttl = 300
records = [data.azurerm_private_endpoint_connection.private-ip2.0.private_service_connection.0.private_ip_address]
}
#------------------------------------------------------------------
# azurerm monitoring diagnostics - Default is "false"
#------------------------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "extaudit" {
count = var.enable_log_monitoring == true && var.log_analytics_workspace_id != null ? 1 : 0
name = lower("extaudit-${var.database_name}-diag")
target_resource_id = azurerm_sql_database.db.id
log_analytics_workspace_id = var.log_analytics_workspace_id
storage_account_id = var.storage_account_id != null ? var.storage_account_id : null
dynamic "log" {
for_each = var.extaudit_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
}
}
lifecycle {
ignore_changes = [log, metric]
}
}