我正在尝试使用element创建一个azure lb规则,这是错误消息和下面列出的代码。
我应该在main.tf文件或variables.tf文件中进行哪些更改以使元素正常工作。但是,当我直接提供端口(已注释)时,代码将执行而没有任何问题。
发生1个错误:
azurerm_lb_rule.test:发生1个错误:
azurerm_lb_rule.test:在第3列第1行:元素:参数1应该为类型列表,类型字符串为:
$ {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"}
}
答案 0 :(得分:0)
根据您的要求,可以将变量lb_port
定义为地图类型,然后使用lookup函数从给定键的地图中检索单个元素的值。
例如,
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}"
...
}
更多示例引用here。