I am trying to create Databricks Workspaces using a module with for_each loop that references a map(object). I am also creating a Private Endpoint in Azure using a separate module for each workspace.
However the issue I am having is in the module for PE we use a dynamic block to create the IP configuration. Which references a map(object) variable with the configuration such as name, private ip address, sub resource name and member name.
In the map(object) I have created three keys with the values for each databricks workspace.
So when calling the Private Endpoint module, I set ipconfigs = var.databricks_pvtep_ip_configs
What I want to happen is the module to loop through each and only use the ip configuration for each workspace. I have tried setting for_each workspaces and each.key but when terraform plan runs it applies all of the ip configuration in the map(object) to all three workspaces. Not one for each.
So really struggling to get this work. A workaround is to call the module three times and set the ip configs variable individually but want to avoid this and call the module once.
Please see the code below
PE Module:
resource "azurerm_private_endpoint" "pvt_ep" {
name = "pvtep-${var.pvt_ep_name}"
location = var.location
resource_group_name = var.rg_name
subnet_id = var.subnet_id
custom_network_interface_name = try(var.custom_network_interface_name, "nic-pvtep-${var.pvt_ep_name}")
dynamic "private_dns_zone_group" {
for_each = var.private_dns_zone_group_setup == true ? [1] : []
content {
name = var.pvt_dnszone_name
private_dns_zone_ids = var.pvt_dnszone_ids
}
}
private_service_connection {
name = var.pvt_svc_conn_name
private_connection_resource_id = var.pvt_conn_res_id
private_connection_resource_alias = var.private_connection_resource_alias
is_manual_connection = var.is_manual_connection
subresource_names = try(var.subresource_ids, null)
request_message = (var.is_manual_connection == true) ? var.request_message : null
}
dynamic "ip_configuration" {
for_each = var.ip_configs
content {
name = ip_configuration.value.ip_config_name # - (Required) Specifies the Name of the IP Configuration. Changing this forces a new resource to be created.
private_ip_address = ip_configuration.value.ip_config_private_ip_address #- (Required) Specifies the static IP address within the private endpoint's subnet to be used. Changing this forces a new resource to be created.
subresource_name = ip_configuration.value.ip_config_subresource_name # (Optional) Specifies the subresource this IP address applies to. subresource_names corresponds to group_id. Changing this forces a new resource to be created.
member_name = ip_configuration.value.ip_config_member_name #
}
}
tags = var.tags_common
}
Module Call:
module "lgr-databricks-workspaces" {
for_each = var.databricks_workspaces
source = "../../LGR-DataPlatform-Databricks/02.LandingZone/02.AzureDatabricks/adb_v1"
databricks_workspace_name = each.value.workspace_name
databricks_rg_name = var.databricks_rg_name
location = var.location
databricks_sku = var.databricks_sku
infrastructure_encryption_enabled = var.infrastructure_encryption_enabled
public_network_access_enabled = var.public_network_access_enabled
network_security_group_rules_required = var.network_security_group_rules_required
managed_rg_name = each.value.managed_rg_workspace_name
no_public_ip = var.no_public_ip
virtual_network_id = var.virtual_network_id
databricks_public_subnet_name = each.value.databricks_public_subnet_name
databricks_private_subnet_name = each.value.databricks_private_subnet_name
public_subnet_network_security_group_association_id = each.value.public_subnet_network_security_group_association_id
private_subnet_network_security_group_association_id = each.value.private_subnet_network_security_group_association_id
tags_common = local.tags_databricks_workspace
}
module "lgr-databricks-workspace-pe" {
source = "./TerraModules/37.PrivateEPModule"
for_each = var.databricks_workspaces
location = var.location
rg_name = var.databricks_rg_name
subnet_id = var.pvtep_subnet_id
pvt_ep_name = each.value.databricks_private_pvt_ep_name
pvt_dnszone_name = local.backend_pvt_dnszone_name
pvt_dnszone_ids = local.backend_pvt_dnszone_ids
pvt_svc_conn_name = each.value.databricks_private_pvt_ep_name
pvt_conn_res_id = module.lgr-databricks-workspaces[each.key].workspace_id
subresource_ids = local.backend_pvt_dnszone_ids
tags_common = local.tags_databricks_backend
ip_configs = var.databricks_pvtep_ip_configs[each.key]
}
Variable:
variable "databricks_pvtep_ip_configs" {
type = map(object({
ip_config_name = string
ip_config_private_ip_address = string
ip_config_subresource_name = string
ip_config_member_name = string
}))
default = {
lgr-workspace1 = {
ip_config_name = "lgr-workspace1-config"
ip_config_private_ip_address = "{{databricks_workspace1_pvtep_ip_address}}"
ip_config_subresource_name = "databricks_ui_api"
ip_config_member_name = "databricks_ui_api"
}
lgr-workspace2 = {
ip_config_name = "lgr-workspace2-config"
ip_config_private_ip_address = "{{databricks_workspace2_pvtep_ip_address}}"
ip_config_subresource_name = "databricks_ui_api"
ip_config_member_name = "databricks_ui_api"
}
lgr-workspace3 = {
ip_config_name = "lgr-workspace3-config"
ip_config_private_ip_address = "{{databricks_workspace3_pvtep_ip_address}}"
ip_config_subresource_name = "databricks_ui_api"
ip_config_member_name = "databricks_ui_api"
}
}
}