首页 > 解决方案 > Terraform,计数时列出错误的字符串

问题描述

我正在尝试制作一个通过循环创建 Azure 路由的 Terraform 剧本。

最终目标是用户将在逗号分隔列表中输入他们的目的地。例如:

Enter route destinations: 0.0.0.0/0,192.168.0.0/16

azurerm_route 将通过此变量运行此函数并创建路由。

variable "destinations" {
  default = "0.0.0.0/0,192.168.0.0/16"
}

resource "azurerm_route" "route" {
  name                    = "route-${count.index}"
  resource_group_name     = "resourcegroup"
  route_table_name        = "table"
  address_prefix          = "${split(",", var.destinations)}[count.index]"
  next_hop_type           = "Internet"
  count                   = "${length(split(",", var.destinations))}"
}

但是,我在通过前缀列表计数时遇到问题,并且收到以下错误:

* azurerm_route.route[1]: At column 1, line 1: output of an HIL 
expression must be a string, or a single list (argument 1 is 
TypeList) in:

${split(",", var.destinations)}[count.index]

* azurerm_route.route[0]: At column 1, line 1: output of an HIL 
expression must be a string, or a single list (argument 1 is 
TypeList) in:

${split(",", var.destinations)}[count.index]

标签: terraform

解决方案


弄清楚了。您必须使用元素插值。

resource "azurerm_route" "route" {
  name                    = "route-${count.index}"
  resource_group_name     = "resourcegroup"
  route_table_name        = "table"
  address_prefix          = "${element(split(",",var.destinations),count.index)}"
  next_hop_type           = "Internet"
  count                   = "${length(split(",",var.destinations))}"
}

推荐阅读