Search code examples
azureazure-bicep

Azure Create an Load Balancer with Bicep


A little background we would like to export all Azure resource templates (Export template) and have a way to recreate the environment if needed. (Eventually, we would like to create a new environment with the template) so I created a temp Load Balancer and exported the template and how trying to recreate it, but every time I fix one error I get another one. At this point, the Load Balancer is created but the Backend Pool does not. I was told I need to reference the server or the virtual Network (resource or module) that the server is in, but I cannot get any info on how.

Backend Servers

Server Name: dev-web01
Network Interface Name: dev-web01-Nic
IP configurations Name ipconfig1, IPv4, Primary, 10.0.0.101 (Static
Subnet Name: dev-web-Net

Server Name: dev-web02
Network Interface Name: dev-web02-Nic
IP configurations Name ipconfig1, IPv4, Primary, 10.0.0.102 (Static)
Subnet Name: dev-web-Net

param location string = 'westus2'

var loadBalancerName = 'dev-web01-LB'
var virtualNetworkName = 'dev-dev-VNET'
var subnetName = 'dev-web-Net'
var subnetRef = resourceId('Microsoft.Network/virtualNetworks/subnets', virtualNetworkName, subnetName)

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2022-07-01' existing = {
  name: virtualNetworkName
}

resource loadBalancers_name 'Microsoft.Network/loadBalancers@2022-07-01' = {
  name: loadBalancerName
  location: location
  sku: {
    name: 'Standard'
    tier: 'Regional'
  }
  properties: {
    frontendIPConfigurations: [
      {
        name: 'FrontendIP'
        id: resourceId('Microsoft.Network/loadBalancers/frontendIpConfigurations', loadBalancerName, 'FrontendIP')
        properties: {
          privateIPAddress: '10.0.0.196'
          privateIPAllocationMethod: 'Static'
          subnet: {
            id: subnetRef
          }
          privateIPAddressVersion: 'IPv4'
        }
      }
    ]
    backendAddressPools: [
      {
        name: 'BackendPool'
        properties: {
          loadBalancerBackendAddresses: [
            {
              // I was told I need to point the server here? (resource or module)
              name: 'dev-web01-Nicipconfig1'
              properties: {
              }
            }
            {
              name: 'dev-web01-Nicipconfig1'
              properties: {
              }
            }
          ]
        }
      }
    ]
    loadBalancingRules: [
      {
        name: 'webport-701'
        id: resourceId('Microsoft.Network/loadBalancers/frontendIpConfigurations', loadBalancerName, 'webport-701')
        properties: {
          frontendIPConfiguration: {
            id: resourceId('Microsoft.Network/loadBalancers/frontendIpConfigurations', loadBalancerName, 'FrontendIP')
          }
          frontendPort: 701
          backendPort: 701
          enableFloatingIP: false
          idleTimeoutInMinutes: 4
          protocol: 'Tcp'
          enableTcpReset: false
          loadDistribution: 'Default'
          disableOutboundSnat: false
          backendAddressPool: {
            id: resourceId('Microsoft.Network/loadBalancers/backendAddressPools', loadBalancerName, 'BackendPool')
          }
          backendAddressPools: [
            {
              id: resourceId('Microsoft.Network/loadBalancers/backendAddressPools', loadBalancerName, 'BackendPool')
            }
          ]
          probe: {
            id: resourceId('Microsoft.Network/loadBalancers/probes', loadBalancerName, 'HealthProbe')
          }
        }
      }
    ]
    probes: [
      {
        name: 'HealthProbe'
        id: resourceId('Microsoft.Network/loadBalancers/probes', loadBalancerName, 'HealthProbe')
        properties: {
          protocol: 'Tcp'
          port: 8080
          intervalInSeconds: 5
          numberOfProbes: 1
          probeThreshold: 1
        }
      }
    ]
    inboundNatRules: []
    outboundRules: []
    inboundNatPools: []
  }
  dependsOn: [
    virtualNetwork
  ]
}

Dose anyone knows how to add Backend Pool using Bicep?


Solution

  • I found a video on creating an ARM template he went through creating a VM in Azure Portal and at the very end instead of creating the VM he click on a Hyperlink at the bottom of the page “Download a template for automation” which allows you to download a JSON file. Then I decompile it and made some modifications and then I was able to create the Load Balancer with a Backend Pool.