问题描述
我正在研究这个 azure_rm nsg(网络安全组)terraform 模块,我正在努力使其尽可能多地驱动和通用。一切都按预期工作,但有一个标志出错。
Main.tf 文件
`resource "azurerm_network_security_rule" "Inbound" {
count = length(var.inbound_port_ranges)
name = "sg-rule-${count.index}"
direction = "Inbound"
access = "Allow"
priority = element(var.priority,count.index)
source_address_prefix = "*"
source_port_range = "*"
destination_address_prefix = "*"
destination_port_range = element(var.inbound_port_ranges,count.index)
protocol = "TCP"
resource_group_name = azurerm_network_security_group.this.resource_group_name
network_security_group_name = azurerm_network_security_group.this.name
}
`
Variables.tf 文件:
`variable "resource_group_name" {
default = "test"
}
variable "priority" {
default = ["100","101"]
}
variable "inbound_port_ranges" {
default = ["8000","8001"]
}
variable "outbound_port_ranges" {
default = ["9000","9001"]
}
`
我能够将列表读入“destination_port_range”的变量中,但不能将优先级变量读入变量中,并且它不断出错并显示以下错误,我不知道为什么?
`Error: Incorrect attribute value type
on main.tf line 20,in resource "azurerm_network_security_rule" "Inbound":
20: priority = "element(var.priority,${count.index})"
|----------------
| count.index is 1
Inappropriate value for attribute "priority": a number is required.
`
如果有人能指出我正确的方向来解决它,那将是一个很大的帮助和高度赞赏。我想要的只是从带有索引的列表中读取值,以便我可以使用相同的入站规则来创建多个规则。
提前致谢。
解决方法
您的 priority
是一个字符串列表。此外,它将是字面上的字符串 "element(var.priority,<number>)"
而不是实际数字。
它应该是一个数字列表:
variable "priority" {
default = [100,101]
}
然后:
priority = element(var.priority,count.index)
据我所知,您将遇到与 destination_port_range
相同的问题。