Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a TCP rule to an existing Azure Load Balancer
#Make sure we are on the correct Azure Subscription
Get-AzContext
$lbName = "your-lb-name-here"
$rgName = "your-resource-group-name"
$probName = "the-lb-probe-name"
$bepName = "backend-pool" ### the backend pool that has the VMs
$port = 10000 ### the port to load balance on the backend pool servers
$ruleName = "rule-name" ###the name of the new rule
$slb = Get-AzLoadBalancer -Name $lbName -ResourceGroupName $rgName
$prb = Get-AzLoadBalancerProbeConfig -Name $probName -LoadBalancer $slb
$beaddpool = Get-AzLoadBalancerBackendAddressPoolConfig -Name $bepName -LoadBalancer $slb
# Add the rule to the configuration
$slb | Add-AzLoadBalancerRuleConfig -Name $ruleName `
-FrontendIPConfiguration $slb.FrontendIpConfigurations[0] `
-BackendAddressPool $beaddpool `
-Protocol "Tcp" `
-FrontendPort $port `
-BackendPort $port `
-Probe $prb
# Set the rule configuration
$slb | Set-AzLoadBalancerRuleConfig -Name $ruleName `
-FrontendIPConfiguration $slb.FrontendIpConfigurations[0] `
-BackendAddressPool $beaddpool `
-Protocol "Tcp" `
-FrontendPort $port `
-BackendPort $port `
-Probe $prb
# Note: you can add couples like the above Add/Set
# so that your rules are added in one GO before
# touching the load balancer once with a single Set
#apply the changes to the load balancer
$slb | Set-AzLoadBalancer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment