Search code examples
azurepowershellazure-load-balancer

powershell command for creating new Inbound NAT Pool of Existing Load Balancer


I want to attach an existing Azure Load Balancer to Virtual Machine Scale Set, for this i need to create inbound nat pool so that i can take the resource ID of the inbound nat pool and pass it to the ARM Template of VMSS. thanks in advance.


Solution

  • According to my research, we can use the PowerShell command New-AzLoadBalancerInboundNatPoolConfig to create an inbound nat pool. For more details, please refer to https://learn.microsoft.com/en-us/powershell/module/az.network/New-AzLoadBalancerInboundNatPoolConfig?view=azps-2.8.0.

    For example:

    Connect-AzAccount
    
    $slb = Get-AzLoadBalancer -Name "testload" -ResourceGroupName "testfun07"
    $feIpConfig = Get-AzLoadBalancerFrontendIpConfig -Name "LoadBalancerFrontEnd" -Loadbalancer $slb
    
    New-AzLoadBalancerInboundNatPoolConfig -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001
    

    enter image description here


    Update

    If we want to use PowerShell to create Inbound NAT Pool for an existing Load balancer, we need to do the following steps

    1. Create a Inbound NAT Pool
    $slb = Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07"
    
    $feIpConfig = Get-AzureRmLoadBalancerFrontendIpConfig -Name "LoadBalancerFrontEnd" -Loadbalancer $slb
    
    Add-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001
    
    1. Configure the ppol for the laad balancer
    Set-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001
    
    
    1. Update the load balancer
    Set-AzureRmLoadBalancer -LoadBalancer $slb
    
    

    For example

    $name = "you account"
    $password = "password"
    $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential ($name, $secpasswd)
    Connect-AzureRmAccount -Credential $mycreds 
    
    $slb = Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07"
    
    $feIpConfig = Get-AzureRmLoadBalancerFrontendIpConfig -Name "LoadBalancerFrontEnd" -Loadbalancer $slb
    
    Add-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001
     "1" | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8
    
    Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07" | Select-Object InboundNatPools | fl | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8
    
    
    
    Set-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001
    
     "2" | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8
    
    Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07" | Select-Object InboundNatPools | fl | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8
    
    
    
    Set-AzureRmLoadBalancer -LoadBalancer $slb
    
    "3" | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8
    
    Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07" | Select-Object InboundNatPools | fl | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8
    

    enter image description here