简体   繁体   中英

How do i connect an azure vm network interface into an azure load balancer address pool?

I have some code which makes multiple vms I'm trying dynamically add them to the load balancer address pool but I'm met with the following error which I have no idea what it means, any help will be appreciated as the error appears to be somewhat obscure

Error: Error: IP Configuration "azure_network_interface_address_pool_association" was not found on Network Interface "client_host_nic-0" (Resource Group "client_rg")

on vm2.tf line 99, in resource "azurerm_network_interface_backend_address_pool_association" "network_interface_backend_address_pool_association": 99: resource "azurerm_network_interface_backend_address_pool_association" "network_interface_backend_address_pool_association" {

vm2.tf file includes

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

load balancer module main.tf file

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

output.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
}

additional information

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

main.tf

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
 }

The error means the Ipconfiguration name you set for the network interface is not the same as you set for the resource azurerm_network_interface_backend_address_pool_association . You can take a look at the description for ip_configuration_name here . And as I see, you want to associate multiple interfaces with the load balancer.

So I recommend you change the network interface and the association like this:

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
}

我相信这是 azure 2.1 terraform provider 的一个错误,根据上游https://github.com/terraform-providers/terraform-provider-azurerm/issues/3794似乎已在 2.33 版中修复

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM