1

我有一些代码可以生成多个 vm 我正在尝试将它们动态添加到负载均衡器地址池中,但是我遇到了以下错误,我不知道这是什么意思,任何帮助将不胜感激,因为错误似乎是有点晦涩难懂

错误:错误:在网络接口“client_host_nic-0”(资源组“client_rg”)上找不到 IP 配置“azure_network_interface_address_pool_association”

在 vm2.tf 第 99 行,在资源“azurerm_network_interface_backend_address_pool_association”“network_interface_backend_address_pool_association”中:99:资源“azurerm_network_interface_backend_address_pool_association”“network_interface_backend_address_pool_association”{

vm2.tf 文件包括

# Create virtual machine
resource "azurerm_network_interface" "client_nics" {
    count                     = var.node_count
    name                      = "client_host_nic-${count.index}"
    location                  = var.resource_group_location
    resource_group_name       = module.network.azurerm_resource_group_client_name
#    network_security_group_id = module.network.bastion_host_network_security_group

    ip_configuration {
        name                          = "client_host_nic"
        subnet_id                     = module.network.client_subnet_id
        private_ip_address_allocation = "Dynamic"
#        public_ip_address_id          = module.network.bastion_host_puplic_ip_address #optional field we have a bastion host so no need for public IP also its vnet peered so this adds an extra layer of securit in a way
    }

    tags = {
        environment = "Production"
    }
}

# Generate random text for a unique storage account name
resource "random_id" "randomId_Generator" {
    keepers = {
        # Generate a new ID only when a new resource group is defined
        resource_group = var.resource_group_location
    }

    byte_length = 8
}


# Create storage account for boot diagnostics
resource "azurerm_storage_account" "client_storageaccount" {
    name                        = "diag${random_id.randomId_Generator.hex}"
    resource_group_name         = module.network.azurerm_resource_group_client_name
    location                    = var.resource_group_location
    account_tier                = "Standard"
    account_replication_type    = "LRS"

    tags = {
        environment = "Production"
    }
}

resource "azurerm_virtual_machine" "node" {
  count                 = var.node_count
  name                  = "client-host-${count.index}"
  location              = var.resource_group_location
  resource_group_name   = module.network.azurerm_resource_group_client_name
  network_interface_ids = ["${element(azurerm_network_interface.client_nics.*.id, count.index)}"]

  # Uncomment this line to delete the OS disk automatically when deleting the VM
   delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
   delete_data_disks_on_termination = true  

  # 1 vCPU, 3.5 Gb of RAM
  vm_size = var.machine_type

  storage_os_disk {
    name              = "myOsDisk-${count.index}"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  os_profile {
        computer_name  = "Production"
        admin_username = "azureuser"
    }

  os_profile_linux_config {
        disable_password_authentication = true
        ssh_keys {
            path     = "/home/azureuser/.ssh/authorized_keys" #This cannot be changed as mentioned in https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.html
            key_data = file("~/.ssh/client.pub")

        }
    }

    boot_diagnostics {
        enabled = "true"
        storage_uri = azurerm_storage_account.client_storageaccount.primary_blob_endpoint
    }

    tags = {
        environment = "Production"
    }
}

resource "azurerm_network_interface_backend_address_pool_association" "network_interface_backend_address_pool_association" {
  count                 = var.node_count
  network_interface_id    = element(azurerm_network_interface.client_nics.*.id, count.index) #fixes interpolation issues
  ip_configuration_name   = "azure_network_interface_address_pool_association"
  backend_address_pool_id = module.loadbalancer.azure_backend_pool_id
}

负载平衡器模块 main.tf 文件

#rember when using this module to call the network module for the resource group name
############## load balancer section  ##############
resource "azurerm_public_ip" "azure_load_balancer_IP" {
  name                = "azure_load_balancer_IP"
  location            = var.resource_group_location
  resource_group_name = var.resource_group_name
  allocation_method   = "Static"
}

resource "azurerm_lb" "azure_load_balancer" {
  name                = "TestLoadBalancer"
  location            = var.resource_group_location
  resource_group_name = var.resource_group_name

  frontend_ip_configuration {
    name                 = "front_end_IP_configuration_for_azure_load_balancer"
    public_ip_address_id = azurerm_public_ip.azure_load_balancer_IP.id
  }
}

resource "azurerm_lb_backend_address_pool" "backend_address_pool" {
  resource_group_name = var.resource_group_name
  loadbalancer_id     = azurerm_lb.azure_load_balancer.id
  name                = "BackEndAddressPool"
}

resource "azurerm_lb_rule" "azure_lb_rule" {
  resource_group_name            = var.resource_group_name
  loadbalancer_id                = azurerm_lb.azure_load_balancer.id
  name                           = "LBRule"
  protocol                       = "Tcp"
  frontend_port                  = 80
  backend_port                   = 80
  frontend_ip_configuration_name = "front_end_IP_configuration_for_azure_load_balancer"
}

输出.tf

output "azure_load_balancer_ip" {
  value = azurerm_public_ip.azure_load_balancer_IP.id
}

output "azure_backend_pool_id" {
  value = azurerm_lb_backend_address_pool.backend_address_pool.id
}

附加信息

* provider.azurerm: version = "~> 2.1"

主文件

module "loadbalancer" {
 source = "./azure_load_balancer_module" #this may need to be a different git repo as we are not referencing branches here only the master
resource_group_name = module.network.azurerm_resource_group_client_name
  resource_group_location = var.resource_group_location
 }
4

2 回答 2

2

该错误意味着您为网络接口设置的Ipconfiguration 名称与您为资源设置的名称不同azurerm_network_interface_backend_address_pool_association。你可以看看ip_configuration_name 这里的描述。正如我所看到的,您希望将多个接口与负载均衡器相关联。

所以我建议你像这样更改网络接口和关联:

resource "azurerm_network_interface" "client_nics" {
    count                     = var.node_count
    name                      = "client_host_nic-${count.index}"
    location                  = var.resource_group_location
    resource_group_name       = module.network.azurerm_resource_group_client_name
#    network_security_group_id = module.network.bastion_host_network_security_group

    ip_configuration {
        name                          = "client_host_nic-${count.index}"
        subnet_id                     = module.network.client_subnet_id
        private_ip_address_allocation = "Dynamic"
#       public_ip_address_id          = module.network.bastion_host_puplic_ip_address #optional field we have a bastion host so no need for public IP also its vnet peered so this adds an extra layer of securit in a way
    }

    tags = {
        environment = "Production"
    }
}

resource "azurerm_network_interface_backend_address_pool_association" "network_interface_backend_address_pool_association" {
  count                 = var.node_count
  network_interface_id    = element(azurerm_network_interface.client_nics.*.id, count.index) #fixes interpolation issues
  ip_configuration_name   = "client_host_nic-${count.index}"
  backend_address_pool_id = module.loadbalancer.azure_backend_pool_id
}
于 2020-03-17T02:51:30.860 回答