вот что работает для меня:
variable "ports" {
"default" = [2379, 6443]
}
resource "azurerm_lb" "lb" {
name = "${var.reference["name"]}-${var.vmName}-lb"
location = "${var.reference["location"]}"
resource_group_name = "${var.reference["name"]}"
count = "${var.vmName != "worker" ? 1 : 0}"
frontend_ip_configuration {
name = "private"
subnet_id = "${var.hack}"
}
}
resource "azurerm_lb_backend_address_pool" "lb-backend" {
resource_group_name = "${var.reference["name"]}"
loadbalancer_id = "${azurerm_lb.lb.id}"
name = "backend"
count = "${var.vmName != "worker" ? 1 : 0}"
}
resource "azurerm_network_interface" "nic" {
name = "${var.reference["name"]}-${var.vmName}-vm-${count.index}-nic"
location = "${var.reference["location"]}"
resource_group_name = "${var.reference["name"]}"
count = "${var.reference["${var.vmName}Count"]}"
depends_on = ["azurerm_lb.lb"]
ip_configuration {
name = "dflt"
subnet_id = "${var.hack}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_network_interface_backend_address_pool_association" "nic-lb-backend" {
network_interface_id = "${element(azurerm_network_interface.nic.*.id, count.index)}"
ip_configuration_name = "dflt"
backend_address_pool_id = "${var.vmName != "worker" ? azurerm_lb_backend_address_pool.lb-backend.id : ""}"
count = "${var.vmName != "worker" ? var.reference["${var.vmName}Count"] : 0}"
depends_on = ["azurerm_network_interface.nic"]
}
resource "azurerm_lb_probe" "probe" {
resource_group_name = "${var.reference["name"]}"
loadbalancer_id = "${azurerm_lb.lb.id}"
name = "${element(var.ports, count.index)}"
port = "${element(var.ports, count.index)}"
count = "${var.vmName != "worker" ? length(var.ports) : 0}"
}
resource "azurerm_lb_rule" "rule" {
resource_group_name = "${var.reference["name"]}"
loadbalancer_id = "${azurerm_lb.lb.id}"
name = "${element(var.ports, count.index)}"
protocol = "Tcp"
frontend_port = "${element(var.ports, count.index)}"
backend_port = "${element(var.ports, count.index)}"
frontend_ip_configuration_name = "private"
backend_address_pool_id = "${var.vmName != "worker" ? azurerm_lb_backend_address_pool.lb-backend.id : ""}"
probe_id = "${var.vmName != "worker" ? element(azurerm_lb_probe.probe.*.id, count.index) : ""}"
count = "${var.vmName != "worker" ? length(var.ports) : 0}"
}
ссылочная переменная:
variable reference {
default = {
"name" = "k9s"
"location" = "ukwest"
"addressSpace" = "10.240.0.0/22"
"etcdCount" = 3
"masterCount" = 3
"workerCount" = 3
}
}