Skip to content

Commit d2cb40c

Browse files
committed
updating private link config to support existing DNS zone, VNet and Subnets
1 parent 782ee6e commit d2cb40c

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

main.tf

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ resource "azurerm_resource_group" "rg" {
2121
tags = merge({ "Name" = format("%s", var.resource_group_name) }, var.tags, )
2222
}
2323

24-
data "azurerm_virtual_network" "vnet01" {
25-
count = var.enable_private_endpoint ? 1 : 0
26-
name = var.virtual_network_name
27-
resource_group_name = local.resource_group_name
28-
}
29-
3024
data "azurerm_client_config" "current" {}
3125

3226
data "azurerm_log_analytics_workspace" "logws" {
@@ -316,11 +310,17 @@ resource "azurerm_sql_failover_group" "fog" {
316310
#---------------------------------------------------------
317311
# Private Link for SQL Server - Default is "false"
318312
#---------------------------------------------------------
313+
data "azurerm_virtual_network" "vnet01" {
314+
count = var.enable_private_endpoint && var.existing_vnet_id == null ? 1 : 0
315+
name = var.virtual_network_name
316+
resource_group_name = local.resource_group_name
317+
}
318+
319319
resource "azurerm_subnet" "snet-ep" {
320-
count = var.enable_private_endpoint ? 1 : 0
321-
name = "snet-endpoint-shared-${local.location}"
322-
resource_group_name = local.resource_group_name
323-
virtual_network_name = var.virtual_network_name
320+
count = var.enable_private_endpoint && var.existing_subnet_id == null ? 1 : 0
321+
name = "snet-endpoint-${local.location}"
322+
resource_group_name = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.resource_group_name : element(split("/", var.existing_vnet_id), 4)
323+
virtual_network_name = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.name : element(split("/", var.existing_vnet_id), 8)
324324
address_prefixes = var.private_subnet_address_prefix
325325
enforce_private_link_endpoint_network_policies = true
326326
}
@@ -330,11 +330,11 @@ resource "azurerm_private_endpoint" "pep1" {
330330
name = format("%s-primary", "sqldb-private-endpoint")
331331
location = local.location
332332
resource_group_name = local.resource_group_name
333-
subnet_id = azurerm_subnet.snet-ep.0.id
333+
subnet_id = var.existing_subnet_id == null ? azurerm_subnet.snet-ep.0.id : var.existing_subnet_id
334334
tags = merge({ "Name" = format("%s", "sqldb-private-endpoint") }, var.tags, )
335335

336336
private_service_connection {
337-
name = "sqldbprivatelink"
337+
name = "sqldbprivatelink-primary"
338338
is_manual_connection = false
339339
private_connection_resource_id = azurerm_sql_server.primary.id
340340
subresource_names = ["sqlServer"]
@@ -346,11 +346,11 @@ resource "azurerm_private_endpoint" "pep2" {
346346
name = format("%s-secondary", "sqldb-private-endpoint")
347347
location = local.location
348348
resource_group_name = local.resource_group_name
349-
subnet_id = azurerm_subnet.snet-ep.0.id
349+
subnet_id = var.existing_subnet_id == null ? azurerm_subnet.snet-ep.0.id : var.existing_subnet_id
350350
tags = merge({ "Name" = format("%s", "sqldb-private-endpoint") }, var.tags, )
351351

352352
private_service_connection {
353-
name = "sqldbprivatelink"
353+
name = "sqldbprivatelink-secondary"
354354
is_manual_connection = false
355355
private_connection_resource_id = azurerm_sql_server.secondary.0.id
356356
subresource_names = ["sqlServer"]
@@ -376,7 +376,7 @@ data "azurerm_private_endpoint_connection" "private-ip2" {
376376
}
377377

378378
resource "azurerm_private_dns_zone" "dnszone1" {
379-
count = var.enable_private_endpoint ? 1 : 0
379+
count = var.existing_private_dns_zone == null && var.enable_private_endpoint ? 1 : 0
380380
name = "privatelink.database.windows.net"
381381
resource_group_name = local.resource_group_name
382382
tags = merge({ "Name" = format("%s", "SQL-Private-DNS-Zone") }, var.tags, )
@@ -386,15 +386,16 @@ resource "azurerm_private_dns_zone_virtual_network_link" "vent-link1" {
386386
count = var.enable_private_endpoint ? 1 : 0
387387
name = "vnet-private-zone-link"
388388
resource_group_name = local.resource_group_name
389-
private_dns_zone_name = azurerm_private_dns_zone.dnszone1.0.name
390-
virtual_network_id = data.azurerm_virtual_network.vnet01.0.id
389+
private_dns_zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
390+
virtual_network_id = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.id : var.existing_vnet_id
391+
registration_enabled = true
391392
tags = merge({ "Name" = format("%s", "vnet-private-zone-link") }, var.tags, )
392393
}
393394

394395
resource "azurerm_private_dns_a_record" "arecord1" {
395396
count = var.enable_private_endpoint ? 1 : 0
396397
name = azurerm_sql_server.primary.name
397-
zone_name = azurerm_private_dns_zone.dnszone1.0.name
398+
zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
398399
resource_group_name = local.resource_group_name
399400
ttl = 300
400401
records = [data.azurerm_private_endpoint_connection.private-ip1.0.private_service_connection.0.private_ip_address]
@@ -403,7 +404,7 @@ resource "azurerm_private_dns_a_record" "arecord1" {
403404
resource "azurerm_private_dns_a_record" "arecord2" {
404405
count = var.enable_failover_group && var.enable_private_endpoint ? 1 : 0
405406
name = azurerm_sql_server.secondary.0.name
406-
zone_name = azurerm_private_dns_zone.dnszone1.0.name
407+
zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
407408
resource_group_name = local.resource_group_name
408409
ttl = 300
409410
records = [data.azurerm_private_endpoint_connection.private-ip2.0.private_service_connection.0.private_ip_address]

variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ variable "private_subnet_address_prefix" {
141141
default = null
142142
}
143143

144+
variable "existing_vnet_id" {
145+
description = "The resoruce id of existing Virtual network"
146+
default = null
147+
}
148+
149+
variable "existing_subnet_id" {
150+
description = "The resource id of existing subnet"
151+
default = null
152+
}
153+
154+
variable "existing_private_dns_zone" {
155+
description = "Name of the existing private DNS zone"
156+
default = null
157+
}
158+
144159
variable "firewall_rules" {
145160
description = "Range of IP addresses to allow firewall connections."
146161
type = list(object({

0 commit comments

Comments
 (0)