main.privateendpoint.tf (74 lines of code) (raw):

# The PE resource when we are managing the private_dns_zone_group block: resource "azurerm_private_endpoint" "this_managed_dns_zone_groups" { for_each = { for k, v in var.private_endpoints : k => v if var.private_endpoints_manage_dns_zone_group } location = each.value.location != null ? each.value.location : var.location name = each.value.name != null ? each.value.name : "pep-${var.name}" resource_group_name = each.value.resource_group_name != null ? each.value.resource_group_name : var.resource_group_name subnet_id = each.value.subnet_resource_id custom_network_interface_name = each.value.network_interface_name tags = each.value.tags private_service_connection { is_manual_connection = false name = each.value.private_service_connection_name != null ? each.value.private_service_connection_name : "pse-${var.name}" private_connection_resource_id = azurerm_automation_account.this.id subresource_names = [each.value.subresource_name] # map to each.value.subresource_name if there are multiple services. } dynamic "ip_configuration" { for_each = each.value.ip_configurations content { name = ip_configuration.value.name private_ip_address = ip_configuration.value.private_ip_address member_name = each.value.member_name != null ? each.value.member_name : each.value.subresource_name # map to each.value.subresource_name if there are multiple services. subresource_name = each.value.subresource_name # map to each.value.subresource_name if there are multiple services. } } dynamic "private_dns_zone_group" { for_each = length(each.value.private_dns_zone_resource_ids) > 0 ? ["this"] : [] content { name = each.value.private_dns_zone_group_name private_dns_zone_ids = each.value.private_dns_zone_resource_ids } } } # The PE resource when we are managing **not** the private_dns_zone_group block: resource "azurerm_private_endpoint" "this_unmanaged_dns_zone_groups" { for_each = { for k, v in var.private_endpoints : k => v if !var.private_endpoints_manage_dns_zone_group } location = each.value.location != null ? each.value.location : var.location name = each.value.name != null ? each.value.name : "pep-${var.name}" resource_group_name = each.value.resource_group_name != null ? each.value.resource_group_name : var.resource_group_name subnet_id = each.value.subnet_resource_id custom_network_interface_name = each.value.network_interface_name tags = each.value.tags private_service_connection { is_manual_connection = false name = each.value.private_service_connection_name != null ? each.value.private_service_connection_name : "pse-${var.name}" private_connection_resource_id = azurerm_automation_account.this.id subresource_names = [each.value.subresource_name] # map to each.value.subresource_name if there are multiple services. } dynamic "ip_configuration" { for_each = each.value.ip_configurations content { name = ip_configuration.value.name private_ip_address = ip_configuration.value.private_ip_address member_name = each.value.member_name != null ? each.value.member_name : each.value.subresource_name # map to each.value.subresource_name if there are multiple services. subresource_name = each.value.subresource_name # map to each.value.subresource_name if there are multiple services. } } lifecycle { ignore_changes = [private_dns_zone_group] } } # Private endpoint application security group associations. # We merge the nested maps from private endpoints and application security group associations into a single map. locals { private_endpoint_application_security_group_associations = { for assoc in flatten([ for pe_k, pe_v in var.private_endpoints : [ for asg_k, asg_v in pe_v.application_security_group_associations : { asg_key = asg_k pe_key = pe_k asg_resource_id = asg_v } ] ]) : "${assoc.pe_key}-${assoc.asg_key}" => assoc } } resource "azurerm_private_endpoint_application_security_group_association" "this" { for_each = local.private_endpoint_application_security_group_associations application_security_group_id = each.value.asg_resource_id private_endpoint_id = azurerm_private_endpoint.this_managed_dns_zone_groups[each.value.pe_key].id }