0
votes

I'm trying to create an azure lb rule using element and here's the error message and the code listed below.

What should I be changing within my main.tf file or variables.tf file to get the element to work. However, when I provide ports directly (which are commented) the code executes without any issues.

1 error(s) occurred:

  • azurerm_lb_rule.test: 1 error(s) occurred:

  • azurerm_lb_rule.test: At column 3, line 1: element: argument 1 should be type list, got type string in:

${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 2)}

main.tf

resource "azurerm_lb_rule" "test" {
  resource_group_name            = "${azurerm_resource_group.test.name}"
  loadbalancer_id                = "${azurerm_lb.lb.id}"
  name                           = "LBRule"
  protocol                       = "Tcp"
  #frontend_port                  = 3389
  #backend_port                   = 3389
 protocol                       = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 0)}"
 frontend_port                  = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 1)}"
 backend_port                   = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 2)}"
  frontend_ip_configuration_name = "${var.frontend_name}"
}

variables.tf

variable "lb_port" {
  description = "Protocols to be used for lb health probes and rules."
  default     = {"var1" = "tcp,3389,3389"}
}
1
try casting it to list? ${tolist(element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 2))} - 4c74356b41

1 Answers

0
votes

According to your requirement, you could define variable lb_port as a map type, then use the lookup function to retrieve the value of a single element from a map given its key.

For example,

variable "lb_port" {
  description = "Protocols to be used for lb health probes and rules."
   default = {
    "protocol" = "TCP"
    "frontend_port" = "3389"
    "backend_port" = "3389"
  }
}
...

resource "azurerm_lb_rule" "test" {
  resource_group_name            = "${azurerm_resource_group.test.name}"
  loadbalancer_id                = "${azurerm_lb.lb.id}"
  name                = "LBRule"
  #protocol            = "Tcp"
  #frontend_port                  = 3389
  #backend_port                   = 3389
  protocol                       = "${lookup(var.lb_port, "protocol" )}"
  frontend_port                  = "${lookup(var.lb_port, "frontend_port" )}"
  backend_port                   = "${lookup(var.lb_port, "backend_port" )}"
  frontend_ip_configuration_name = "${var.frontend_name}"
  ...
}

More example references here.