Search code examples
azureterraforminfrastructure-as-code

Terraform private azure load balancer issue


Iam trying to deploy an infrastructure with a private loadbalancer:

.....
resource "azurerm_lb" "private" {
name                = "${var.name}-${var.live}-private-lb"
location            = data.azurerm_resource_group.rg.location
resource_group_name = data.azurerm_resource_group.rg.name
sku                 = var.sku

frontend_ip_configuration {
  name                          = "frontend"
  subnet_id                     = var.subnet_id != "" ? var.subnet_id : null
  private_ip_address            = (var.subnet_id != "" && var.private_ip != "") ? var.private_ip : null
  private_ip_address_allocation = var.subnet_id != "" ? (var.subnet_id == "" ? "Static" : "Dynamic") : null
 }
}
......

But i got the error message :

..../frontendIPConfigurations/frontend must reference either a Subnet, Public IP Address or Public IP Prefix." Details=[]

Why and how can i tackle this issue ? I don't know which configuration is missing. thanks


Solution

  • An internal Load Balancer differs from a public Load Balancer, it has been assigned to a subnet and does not have a public IP address. As the error displayed, the frontend should reference either a Subnet, Public IP Address or Public IP Prefix, and the subnet should have existed when you reference. You could use the data source subnet to access information about an existing resource or create your subnet and VNet for your load balancer.

    For example, the following can work for me.

    data "azurerm_resource_group" "rg" {
      name     = "mytestrg" 
    }
    
    
    variable "sku" {
      default = "basic"
    }
    
    variable "private_ip" {
      default = "172.19.0.100"
    }
    
    variable "env" {
      default="Static"
    }
    
    data "azurerm_subnet" "test" {
      name                 = "default"
      virtual_network_name = "vnet1"
      resource_group_name  = "${data.azurerm_resource_group.rg.name}"
    }
    
    resource "azurerm_lb" "test" {
      name                = "mytestlb"
      location            = "${data.azurerm_resource_group.rg.location}"
      resource_group_name = "${data.azurerm_resource_group.rg.name}"
      sku                 = "${var.sku}"
    
      frontend_ip_configuration {
        name                          = "frontend"
        subnet_id                     = "${data.azurerm_subnet.test.id}"
        private_ip_address            = "${var.env=="Static"? var.private_ip: null}"
        private_ip_address_allocation = "${var.env=="Static"? "Static": "Dynamic"}"
      }
    }