The issue I'm running into is with regard to the load balancer front end ip so it can use a unique Public IP to reach a different server in a different Availability Zone. I'm using count on creating the Public IPs, but I'm not using count on the load balancer because I don't want a new LB for each server. If i could somehow save the Public IPs to a variable then i could reference them using for_each inside the dynamic block but I can't find a way to do that. Here is the code i have so far, but it can't work as is. There may not be a solution to this problem, which really stinks. BTW I'm using the split function below so it returns a list that the properties need. It's a bit hackish but it does work.
resource "azurerm_public_ip" "pip" {
count = "${var.nblinuxvms}"
name = "${var.proj_name}-lbpip${count.index}-${var.region}-${var.app_env}"
location = var.region
resource_group_name = "${azurerm_resource_group.rg.name}"
allocation_method = "Static" #Public IP Standard SKUs require allocation_method to be set to Static
sku = "Standard" #Standard SKU Required for Zones
domain_name_label = "${var.proj_name}${count.index}${split("", "${element(["1", "2", "3"], "${count.index}")}")}"
zones = "${var.avzones}" ? split("", "${element(["1", "2", "3"], "${count.index}")}") : null
}
resource "azurerm_lb" "lb" {
name = "externallb"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
sku = "standard" #standard SKU needed to support zones
dynamic "frontend_ip_configuration" {
for_each = "${azurerm_public_ip.test.*.ip_address}" #this is the problem line. I need a way to store all the IPs in a variable and then iterate through them for each new frontend ip configuration
content{
name = "primary${count.index}" #This name is also important as this is how I'll connect the nat rule down below
public_ip_address_id = "${azurerm_public_ip.pip.id}"
}
resource "azurerm_lb_nat_rule" "lbnr" {
count = "${var.nblinuxvms}"
resource_group_name = "${azurerm_resource_group.rg.name}"
loadbalancer_id = "${azurerm_lb.lb.id}"
name = "SSHHost${count.index}"
protocol = "Tcp"
frontend_port = "${2200 + count.index}"
backend_port = 22
frontend_ip_configuration_name = "primary${count.index}" #This name needs to match the LB Front End IP Configuartion
}
The frontend_ip_configuration_name needs to match the load balancer name. Dynamic block with for_each seems like the best solution for the particular issue as its not a resource...but I don't see a way to save the public ip to any variables i can reference. If there isn't a solution how are people solving this? By creating a separate LB per Azure availability zone? Since it has to be a standard, not basic LB that seems cost prohibitive. Hopefully I've just missed something. Any help would be greatly appreciated. Note i have only shared the relevent code from my terraform project. If more code is needed please let me know.(I couldn't add dynamic block to the question tag because my rep is to low.) Thanks, -Sam Kachar