I am trying to create multiple frontend_ip_configuration for azurerm_lb.
Below is my setup for this:
1. Tfvars file:
loadbalancers2 = [
{ # First LB
name = "test"
location = "eastus"
resource_group_name = "myrg"
subnet = "test"
frontend_private_ips = [
{
fpip_config_name_suffix = "FIP01"
private_ip_address = "10.202.1.12"
},
{
fpip_config_name_suffix = "FIP02"
private_ip_address = "10.202.1.13"
}
]
..
},
]
2. Locals & Resource:
locals {
alb_frontend_config_list = flatten([
for alb in var.loadbalancers2 : [
for fend in alb.frontend_private_ips : {
fpip_config_name_suffix = fend.fpip_config_name_suffix
fpip_config_name = "${alb.name}-${fend.fpip_config_name_suffix}"
private_ip_address = fend.private_ip_address
}
]
])
}
resource "azurerm_lb" "loadbalancers2" {
for_each = { for alb in var.loadbalancers2 : alb.name => alb }
name = each.value.name
location = each.value.location
..
dynamic "frontend_ip_configuration" {
for_each = local.alb_frontend_config_list # ??
iterator = fend # ??
content {
name = fend.fpip_config_name # Does not works
subnet_id = data.azurerm_subnet.subnets[each.value.subnet].id
private_ip_address = ( fend.value != "" ? fend.value : null )
private_ip_address_allocation = ( fend.value != "" ? "Static" : "Dynamic" )
}
}
}
I want to do something like this. How do I achieve this:
frontend_ip_configuration {
name = "${each.value.name}-${each.value.frontend_private_ips[0].fpip_config_name_suffix}"
subnet_id = data.azurerm_subnet.subnets[each.value.subnet].id
private_ip_address = ( each.value.frontend_private_ips[0].private_ip_address != "" ? each.value.frontend_private_ips[0].private_ip_address : null )
private_ip_address_allocation = ( each.value.frontend_private_ips[0].private_ip_address != "" ? "Static" : "Dynamic" )
}
frontend_ip_configuration {
name = "${each.value.name}-${each.value.frontend_private_ips[1].fpip_config_name_suffix}"
subnet_id = data.azurerm_subnet.subnets[each.value.subnet].id
private_ip_address = ( each.value.frontend_private_ips[1].private_ip_address != "" ? each.value.frontend_private_ips[1].private_ip_address : null )
private_ip_address_allocation = ( each.value.frontend_private_ips[1].private_ip_address != "" ? "Static" : "Dynamic" )
}
To meet your goal of creating multiple
frontend_ip_configurationentries for an Azure Load Balancer (azurerm_lb) in Terraform, you'll need to fine-tune your method to ensure the dynamic block properly iterates over thefrontend_private_ipsfor each load balancer. The current structure of yourlocalsandresourceblock requires some adjustments to function as expected.alb_frontend_config_listlocal flattens all frontend configurations into a single list without associating them back to their respective load balancers, which makes it challenging to apply them correctly in thedynamicblock within theazurerm_lbresource.dynamicblock within theazurerm_lbresource should iterate overfrontend_private_ipsfor each load balancer, but the existing setup does not facilitate this due to the mentioned flattening.I make necessary changes in the code and rewrite it as per the requirement for multiple load balancers.
My terraform configuration:
tfvars:
deployment succeeded: