terraformterraform-provider-azureazure-load-balancer

How to use existing VMs in the Backend Pool while creating a new Internal Load Balancer in Azure via terraform


Currently I'm trying to create a new Internal Load Balancer, which uses existing VMs already present in Azure. I was looking on how i can include these VMs data in the Terraform script. I did research a bit on this and found some have done it through NAT association, which doesn't work in my case, as i'm trying to deploy an Internal Load Balancer.

Please assist.

Current code:


data "azurerm_virtual_network" "vnet0001" {
  name                = "vnet0001"
  resource_group_name = "vnet-rg"
}

data "azurerm_subnet" "sub" {
  name                = "vnet00001-sub"
  resource_group_name = "vnet-rg"
}

resource "azurerm_lb" "terra-app" {
  name                = "lb-dev"
  location            = "East US2"
  resource_group_name = azurerm_resource_group.terra-app.name
  sku                 = "Standard"
  sku-tier            = "Regional"

  frontend_ip_configuration {
    name = "frontendip"
    zones = ["1", "2", "3"]
    subnet_id = azurerm_subnet.sub.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_lb_backend_address_pool" "terra-app" {
  loadbalancer_id = azurerm_lb.terra-app.id
  name            = "lb-backendpool"
}

resource "azurerm_lb_probe" "h-probe" {
  loadbalancer_id   = azurerm_lb.terra-app.id
  name              = "hp1"
  port              = 111
  protocol          = "Tcp"
  request_path      = "/"
}

resource "azurerm_lb_rule" "lb-rule" {
  loadbalancer_id                = azurerm_lb.terra-app.id
  name                           = "lb-r1"
  protocol                       = "Tcp"
  frontend_port                  = 111
  backend_port                   = 111
  frontend_ip_configuration_name = "frontendip"
  backend_address_pool_ids       = azurerm_lb_backend_address_pool.terra-app.id
  probe_id                       = azurerm_lb_probe.terra-ipp.id
}

Solution

  • I tried to reproduce the same issue in my environment and got the below results

    I have added the some script for below file

    provider "azurerm" {
      features {}
    }
    
    resource "azurerm_resource_group" "my-rg-test" {
      name     = "RG-Terraform-on-Azure"
      location = "West Europe"
    }
    
    resource "azurerm_virtual_network" "my-vnet-test" {
      name                = "example-vnet"
      address_space       = ["10.0.0.0/16"]
      location            = azurerm_resource_group.my-rg-test.location
      resource_group_name = azurerm_resource_group.my-rg-test.name
    }
    
    resource "azurerm_subnet" "example" {
      name                 = "example-subnet"
      resource_group_name  = azurerm_resource_group.my-rg-test.name
      virtual_network_name = azurerm_virtual_network.my-vnet-test.name
      address_prefixes     = ["10.0.2.0/24"]
      service_endpoints    = ["Microsoft.Storage"]
    }
        resource "azurerm_lb" "terra-app" {
          name                = "lb-dev"
          location            = "East US2"
          resource_group_name = azurerm_resource_group.terra-app.name
          sku                 = "Standard"
          sku-tier            = "Regional"
          
          frontend_ip_configuration {
            name = "frontendip"
            zones = ["1", "2", "3"]
            subnet_id = azurerm_subnet.sub.id
            private_ip_address_allocation = "Dynamic"!
          }
        }
        
        resource "azurerm_lb_backend_address_pool" "terra-app" {
          loadbalancer_id = azurerm_lb.terra-app.id
          name            = "lb-backendpool"
        }
        
        resource "azurerm_lb_probe" "h-probe" {
          loadbalancer_id   = azurerm_lb.terra-app.id
          name              = "hp1"
          port              = 111
          protocol          = "Tcp"
          request_path      = "/"
        }
        
        resource "azurerm_lb_rule" "lb-rule" {
          loadbalancer_id                = azurerm_lb.terra-app.id
          name                           = "lb-r1"
          protocol                       = "Tcp"
          frontend_port                  = 111
          backend_port                   = 111
          frontend_ip_configuration_name = "frontendip"
          backend_address_pool_ids       = azurerm_lb_backend_address_pool.terra-app.id
          probe_id                       = azurerm_lb_probe.terra-ipp.id
        }
    

    I have used the some commands to execute the file

    terraform init
    

    By using this command It will initialize the configuration file

    enter image description here

    terraform plan
    

    By using this command it will determine the what needs to be created or update or destroy to move from current state to desired state

    enter image description here

    terraform apply
    

    By using this command it will perform the changes required to reach the desired state

    enter image description here enter image description here

    When I open the portal I am able to see the load balancer which I have newly created

    enter image description here

    enter image description here