Search code examples
azureterraformazure-load-balancer

How to add a backend with virtual machine IP for Azure Load Balancer in Terraform


I am creating an Azure load balancer that I need to route the incoming traffic to backend pool which consist of a virtual machine.

I am using Terraform to do this.

So far, I have created the followings:

  1. Load Balancer
  2. Load Balancer Rule
  3. Backend Address Pool
  4. A health probe

But in the terraform for backend, there's no way I can setup the IP address of the VM.

Here's my code. How can I do this?

resource "azurerm_lb" "example" {
  name                = "TestLoadBalancer"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  sku                 = "Standard"

  frontend_ip_configuration {
    name                          = "classiclb"
    subnet_id                     = azurerm_subnet.vm.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_lb_backend_address_pool" "example" {
  loadbalancer_id = azurerm_lb.example.id
  name            = "classiclb"
}

resource "azurerm_lb_probe" "example" {
  resource_group_name = azurerm_resource_group.example.name
  loadbalancer_id     = azurerm_lb.example.id
  name                = "classiclb"
  port                = 80
  interval_in_seconds = 10
  number_of_probes    = 3
  protocol            = "Http"
  request_path        = "/"
}

resource "azurerm_lb_rule" "example" {
  resource_group_name            = azurerm_resource_group.example.name
  loadbalancer_id                = azurerm_lb.example.id
  name                           = "classiclb"
  protocol                       = "Tcp"
  frontend_port                  = 80
  backend_port                   = 80
  frontend_ip_configuration_name = "classiclb"
  backend_address_pool_id        = azurerm_lb_backend_address_pool.example.id
  probe_id                       = azurerm_lb_probe.example.id
}

Solution

  • Based on the comments, the addresses are added using azurerm_lb_backend_address_pool_address.