1

我正在研究这个 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
}

`

变量.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.
`

如果有人能指出我正确的方向来解决它,那将是一个很大的帮助和高度赞赏。我想要的只是从带有索引的列表中读取值,以便我可以使用相同的入站规则来创建多个规则。

提前致谢。

4

1 回答 1

1

priority是一个字符串列表。此外,它将是字面上的字符串,"element(var.priority, <number>)"而不是实际的数字。

它应该是一个数字列表:

variable "priority" {
  default = [100, 101]
}

进而:

priority                   = element(var.priority, count.index)

destination_port_range据我所知,你会有同样的问题。

于 2020-12-20T04:07:25.333 回答