0

我是 HCL 和 Terraform 的新手,在将安全组和后端地址池与网络接口相关联时遇到问题。我在单个网络接口块中创建 2 个网络接口:

#Create network interface for 2 VMs
resource "azurerm_network_interface" "FrontNetworkInterface" {
    count = 2
    name = "niFront${count.index}"
    location = azurerm_resource_group.PWSDevResourceGroup.location
    resource_group_name = azurerm_resource_group.PWSDevResourceGroup.name

    ip_configuration {
        name = "ipconfFrontVM"
        subnet_id = azurerm_subnet.PWSDevSubnet.id
        private_ip_address_allocation = "dynamic"
    }
}

我尝试过以产生不同错误的各种方式进行关联:

尝试 1:

#Connect security group to the network interface
resource "azurerm_network_interface_security_group_association" "PWSDevSecurityGroupAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface.id
    network_security_group_id = azurerm_network_security_group.PWSDevSecurityGroup.id
}

#Connect 2 backend ips to the load balancer
resource "azurerm_network_interface_backend_address_pool_association" "BackendIPAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface.id
    ip_configuration_name = "bipa"
    backend_address_pool_id = azurerm_lb_backend_address_pool.BackendIpPool.id
}

错误:

错误:在资源“azurerm_network_interface_security_group_association”“PWSDevSecurityGroupAssoc”中的 front.tf 第 85 行缺少资源实例键:85:network_interface_id = azurerm_network_interface.FrontNetworkInterface.id 因为 azurerm_network_interface.FrontNetworkInterface 设置了“计数”,所以必须在特定实例上访问其属性。例如,要与引用资源的索引相关联,请使用:azurerm_network_interface.FrontNetworkInterface[count.index]

错误:front.tf 第 91 行缺少资源实例键,在资源“azurerm_network_interface_backend_address_pool_association”“BackendIPAssoc”中:91:network_interface_id = azurerm_network_interface.FrontNetworkInterface.id 因为 azurerm_network_interface.FrontNetworkInterface 设置了“count”,所以必须在特定实例上访问其属性。例如,要与引用资源的索引相关联,请使用:azurerm_network_interface.FrontNetworkInterface[count.index]

ATTEMPT 2/3/4(使用“[count.index]”、“[count.index].id”或“[element(azurerm_network_interface.FrontNetworkInterface.*.id, count.index)]”,如前面所述错误):

#Connect security group to the network interface
resource "azurerm_network_interface_security_group_association" "PWSDevSecurityGroupAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index]
    network_security_group_id = azurerm_network_security_group.PWSDevSecurityGroup.id
}

#Connect 2 backend ips to the load balancer
resource "azurerm_network_interface_backend_address_pool_association" "BackendIPAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index]
    ip_configuration_name = "bipa"
    backend_address_pool_id = azurerm_lb_backend_address_pool.BackendIpPool.id
}

错误([count.index].id 和 [element(azurerm_network_interface.FrontNetworkInterface.*.id, count.index)] 的结果相同):

错误:在资源“azurerm_network_interface_security_group_association”“PWSDevSecurityGroupAssoc”中的 front.tf 第 85 行的非计数上下文中引用“count”:85:network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index] “count”对象只能用于“模块”、“资源”和“数据”块,并且仅当设置了“计数”参数时。

错误:在资源“azurerm_network_interface_backend_address_pool_association”“BackendIPAssoc”中的非计数上下文front.tf第91行中引用“count”:network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index]“count”对象只能在“module”中使用、“资源”和“数据”块,并且仅当设置了“计数”参数时。

此外,我在我的 azurerm_virtual_machine 块上收到此错误:

第 162 行,在资源 "azurerm_virtual_machine" "FrontEndVirtualMachines": 162: admin_ssh_key { 此处不需要“admin_ssh_key”类型的块。

我正在关注此处显示的内容:

https://docs.microsoft.com/en-us/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure

如您所见,提供了 admin_ssh_key 块。我尝试使用脚本中使用的 2.0 版;但是,我遇到了同样的结果。

谢谢你的帮助!!:)

4

2 回答 2

0

引用使用 count 创建的资源时,您仍然需要添加 .id。请参阅以下示例。有关详细信息,请参阅此链接

provider "azurerm" {
  version = "~>2.23.0"
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_virtual_network" "example" {
  name                = "vnet"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
  dns_servers         = ["10.0.0.4", "10.0.0.5"]
}

resource "azurerm_subnet" "example" {
  name                 = "example"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "example" {
  count               = 2
  name                = format("int%s", count.index)
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "ip"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "dynamic"
  }
}

resource "azurerm_network_security_group" "example" {
  name                = "acceptanceTestSecurityGroup1"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  security_rule {
    name                       = "test123"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "*"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

resource "azurerm_network_interface_security_group_association" "secgroup" {
  count                     = length(azurerm_network_interface.example)
  network_interface_id      = azurerm_network_interface.example[count.index].id
  network_security_group_id = azurerm_network_security_group.example.id
}
于 2020-09-17T11:31:50.667 回答
0

我承认我没有阅读整个故事,但看起来你的尝试#2/3/4 非常接近。在你使用的地方[count.index],你需要指定 a count,否则没有计数来索引。因此,如果您只是添加count = 2到这两个资源块,它应该可以工作。

更好的是,要么将其2作为变量,要么使用

count = len(azurerm_network_interface.FrontNetworkInterface)

以确保您在2以后更改时不会得到不匹配的数字。

于 2020-09-16T20:50:20.063 回答