无法通过Terraform创建和附加公共IP到VM

时间:2019-05-27 04:25:41

标签: terraform terraform-provider-azure

尝试创建公共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'

我的Terraform代码如下:

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}"
}
}

我无法理解问题所在,我专门在网络接口ip配置块中添加了“ depends_on”,如下所示:

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

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

它报告如下错误,

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

我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

关于该错误,由于您的资源“ azurerm_network_interface”已计数,因此您可以使用element函数像这样更改public_ip_address_id

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)}"