本文介绍了无法通过 terraform 创建和附加公共 ip ro 虚拟机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试创建公共 ip 时出现以下错误

I am getting the below error when I try to create a public ip

2019-05-27T03:07:44.5185437Z * azurerm_network_interface.tf-ni-erx-sqlcl1[1]: Resource 'azurerm_public_ip.tf-pip-erx-sqlcl1' not found for variable 'azurerm_public_ip.tf-pip-erx-sqlcl1.id'
2019-05-27T03:07:44.5186152Z * azurerm_network_interface.tf-ni-erx-sqlcl1[0]: Resource 'azurerm_public_ip.tf-pip-erx-sqlcl1' not found for variable 'azurerm_public_ip.tf-pip-erx-sqlcl1.id'
2019-05-27T03:07:44.5186473Z * azurerm_network_interface.tf-ni-erx-sqlcl2: 2 error(s) occurred:
2019-05-27T03:07:44.5186558Z
2019-05-27T03:07:44.5186910Z * azurerm_network_interface.tf-ni-erx-sqlcl2[0]: Resource 'azurerm_public_ip.tf-pip-erx-sqlcl2' not found for variable 'azurerm_public_ip.tf-pip-erx-sqlcl2.id'
2019-05-27T03:07:44.5187360Z * azurerm_network_interface.tf-ni-erx-sqlcl2[1]: Resource 'azurerm_public_ip.tf-pip-erx-sqlcl2' not found for variable 'azurerm_public_ip.tf-pip-erx-sqlcl2.id'

我的地形代码如下:

resource "azurerm_public_ip" "tf-pip-erx-sqlcl1" {
  count                     = "${var.count_sqlcl1_vm}"
  name                      = "${var.sql_base_hostname}${format("%02d",count.index+1)}-pip01"
  location                  = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
  resource_group_name       = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
  allocation_method         = "Dynamic"
}
# Network inteface for sql
resource "azurerm_network_interface" "tf-ni-erx-sqlcl1" {
 count               = "${var.count_sqlcl1_vm}"
 name                = "${var.bussvc_base_hostname}${format("%02d",count.index+1)}-nic01"
 location            = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
 resource_group_name = "${data.azurerm_resource_group.tf-rg-erx-external.name}"

ip_configuration {
    name                          = "${var.sql_base_hostname}${format("%02d",count.index+1)}-iip01"
    subnet_id                     = "${data.azurerm_subnet.tf-sn-erx-sql.id}"
    private_ip_address_allocation = "${var.env=="msdn"?"dynamic":"static"}"
    #private_ip_address            = "${var.env=="msdn"?"":"10.112.3.${count.index+10}"}"
    public_ip_address_id          = "${azurerm_public_ip.tf-pip-erx-sqlcl1.id}"
}
}
resource "azurerm_public_ip" "tf-pip-erx-sqlcl2" {
  count                     = "${var.count_sqlcl2_vm}"
  name                      = "${var.sql_base_hostname}${format("%02d",count.index+1)}-pip01"
  location                  = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
  resource_group_name       = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
  allocation_method         = "Dynamic"
}
resource "azurerm_network_interface" "tf-ni-erx-sqlcl2" {
 count               = "${var.count_sqlcl2_vm}"
 name                = "${var.sql_base_hostname}${format("%02d%s",count.index,var.count_sqlcl1_vm)}-nic01"
 location            = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
 resource_group_name = "${data.azurerm_resource_group.tf-rg-erx-external.name}"

ip_configuration {
    name                          = "${var.sql_base_hostname}${format("%02d",count.index+1)}-iip01"
    subnet_id                     = "${data.azurerm_subnet.tf-sn-erx-sql.id}"
    private_ip_address_allocation = "${var.env=="msdn"?"dynamic":"static"}"
    #private_ip_address           = "10.112.3.${count.index+15}"
    public_ip_address_id          = "${azurerm_public_ip.tf-pip-erx-sqlcl2.id}"
}
}

我无法理解是什么问题,我专门在网络接口中添加了depends_on",如下所示的ip配置块

I'm unable to understand what is the issue, I specifically added "depends_on" to network interface , ip configuration block like below

depends_on                    = "[azure_public_ip.tf-pip-erx-sqlcl2]"

这样做的想法是确保在创建网络接口资源之前创建公共 ip,但不幸的是,这也无济于事.

The idea behind to do so is to ensure public ip is created before network interface resource is created but unfortunately that does not help either.

报错如下,

Error: azurerm_network_interface.tf-ni-erx-sqlcl2[1]: ip_configuration.0: invalid or unknown key: depends_on

非常感谢任何帮助.

推荐答案

关于错误,因为你的资源azurerm_network_interface"有计数,你可以像这样用 功能:

About the error, since your resource "azurerm_network_interface" has count, you could change the public_ip_address_id like this with element function:

public_ip_address_id          = "${element(azurerm_public_ip.tf-pip-erx-sqlcl1.*.id,count.index)}"

public_ip_address_id          = "${element(azurerm_public_ip.tf-pip-erx-sqlcl2.*.id,count.index)}"

这篇关于无法通过 terraform 创建和附加公共 ip ro 虚拟机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:38