无法通过terraform在VM上创建和连接公共ip

无法通过terraform在VM上创建和连接公共ip,terraform,terraform-provider-azure,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

当我试图创建一个公共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}"
}
}
我无法理解是什么问题,我特别在网络接口、ip配置块中添加了“依赖”,如下所示

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

非常感谢您的帮助。

关于此错误,由于您的资源“azurerm\u network\u interface”已计数,您可以使用以下功能更改
公共ip\u地址\u 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)}"