【问题标题】:terraform reporting error "cannot use element function with an empty list"terraform 报告错误“无法使用带有空列表的元素函数”
【发布时间】:2020-02-10 14:18:59
【问题描述】:

我的网卡创建代码如下:

resource "azurerm_network_interface" "tf-ni-test" {
 count                     = "${var.count_sss_vm}"
 name                      = "${var.sss_base_hostname}${format("%02d",count.index+1)}-nic01"
 #location                 = "${data.azurerm_resource_group.tf-rg-test-external.location}"
 location                  = "${var.location}"  
 resource_group_name       = "${data.azurerm_resource_group.tf-rg-test-external.name}"

 ip_configuration {
    name                          = "${var.sss_base_hostname}${format("%02d",count.index+1)}-iip01"
    subnet_id                     = "${data.azurerm_subnet.tf-sn-test.id}"
    private_ip_address_allocation = "${var.env=="msdn"?"dynamic":"static"}"
    #private_ip_address            = "10.112.2.${count.index+10}"
    public_ip_address_id          = "${element(azurerm_public_ip.tf-pip-test.*.id,count.index+1)}"
 }
}

VM创建代码如下:

resource "azurerm_virtual_machine" "tf-vm-test" {
  count                 = "${var.count_sss_vm}"
  name                  = "${var.sss_base_hostname}${format("%02d",count.index+1)}"
  location              = "${var.location}"  
  #location              = "${data.azurerm_resource_group.tf-rg-test-external.location}"
  resource_group_name   = "${data.azurerm_resource_group.tf-rg-test-external.name}"
  network_interface_ids = ["${element(azurerm_network_interface.tf-ni-test.*.id, count.index)}"]
  vm_size               = "${var.sss_vm_size}"
.
.
.
}

我正在使用以下命令运行 terraform 计划:

terraform plan -var "application_nsg=test-sss" -var "count_sss_vm=2" -var "env=msdn" -var "username=devopsadmin" -var "password=Angular1@#$" -var "sss_base_hostname=testsss" -var "sss_vm_size=Standard_B2s" -var "storage_account_suffix=sta" -var "win_image_offer=Windows" -var "win_image_publisher=microsoftvisualstudio" -var "win_sku=Windows-10-N-x64" -var "location=australiaeast"

很想知道它为什么报告错误以及如何解决它。同样的代码以前也有。这是因为 TF v0.12 的原因吗?

我在 github 上发现了类似的问题,实际上使用了 Hashicorp 给出的相同方法: https://github.com/terraform-providers/terraform-provider-azurerm/issues/3979

我收到如下错误:

Error: Error in function call

  on frednxt_tpi_standalone.tf line 123, in resource "azurerm_virtual_machine" "tf-vm-test":
 123:   network_interface_ids = ["${element(azurerm_network_interface.tf-ni-test.*.id, count.index)}"]
    |----------------
    | azurerm_network_interface.tf-ni-test is empty tuple
    | count.index is 0

Call to function "element" failed: cannot use element function with an empty
list.


Error: Error in function call

  on frednxt_tpi_standalone.tf line 123, in resource "azurerm_virtual_machine" "tf-vm-test":
 123:   network_interface_ids = ["${element(azurerm_network_interface.tf-ni-test.*.id, count.index)}"]
    |----------------
    | azurerm_network_interface.tf-ni-test is empty tuple
    | count.index is 1

Call to function "element" failed: cannot use element function with an empty
list.

【问题讨论】:

  • 您能否提供有关您遇到的错误的消息?
  • 愚蠢的我忘了把实际的错误信息。现在完成。

标签: azure-virtual-machine terraform-provider-azure


【解决方案1】:

实际上不确定,但是当我使用 0.12 版的 terraform 时,我重现了该错误。当我使用命令terraform 0.12upgrade 升级 Terraform 时,它工作正常。而且我在您提供的代码中也找不到问题。

所以你可以删除状态文件,升级 Terraform,然后再试一次。

【讨论】:

  • 我已经在使用 terraform v0.12.5。我重试了,它没有删除状态或备份文件就消失了。真奇怪。再次衷心感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 2020-07-01
相关资源
最近更新 更多