How to get the port mapping for Azure VM in VMSS with Load Balancer dynamic pool

JAMES MCCLELLAN 20 Reputation points
2025-08-22T22:06:02.44+00:00

I have a Virtual Machine Scale Set (VMSS) with a load balancer with a single public IP, the load balancer is dynamically assigning a public port for each VM instance in the VMSS, so each instance can be accessed directly with the same public IP and different port.

In an azure function (c# .NET) how can I get that port mapping information to know what the external public port number is for a given VM instance in the VMSS?

Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
{count} votes

Answer accepted by question author
  1. Ankit Yadav 4,145 Reputation points Microsoft External Staff Moderator
    2025-08-22T22:28:37.3533333+00:00

    Hello JAMES MCCLELLAN,

    Looks like you are trying to get inboundNatRulePortMappings from the LoadBalancer programmatically.

    You could you a rest api call with the details to get the port mappings. For example:

    GET  https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/backendAddressPools/{backendPoolName}/queryInboundNatRulePortMapping?api-version=2024-05-01
    

    or if it's specific to (C# code), you can refer below example code

    using Azure;
    using Azure.ResourceManager;
    using System;
    using System.Threading.Tasks;
    using Azure.Core;
    using Azure.Identity;
    using Azure.ResourceManager.Network.Models;
    using Azure.ResourceManager.Network;
    
    // Generated from example definition: specification/network/resource-manager/Microsoft.Network/stable/2024-05-01/examples/QueryInboundNatRulePortMapping.json
    // this example is just showing the usage of "LoadBalancers_ListInboundNatRulePortMappings" operation, for the dependent resources, they will have to be created separately.
    
    // get your azure access token, for more details of how Azure SDK get your access token, please refer to https://free.blessedness.top/en-us/dotnet/azure/sdk/authentication?tabs=command-line
    TokenCredential cred = new DefaultAzureCredential();
    // authenticate your client
    ArmClient client = new ArmClient(cred);
    
    // this example assumes you already have this BackendAddressPoolResource created on azure
    // for more information of creating BackendAddressPoolResource, please refer to the document of BackendAddressPoolResource
    string subscriptionId = "subid";
    string groupName = "rg1";
    string loadBalancerName = "lb1";
    string backendPoolName = "bp1";
    ResourceIdentifier backendAddressPoolResourceId = BackendAddressPoolResource.CreateResourceIdentifier(subscriptionId, groupName, loadBalancerName, backendPoolName);
    BackendAddressPoolResource backendAddressPool = client.GetBackendAddressPoolResource(backendAddressPoolResourceId);
    
    // invoke the operation
    QueryInboundNatRulePortMappingContent content = new QueryInboundNatRulePortMappingContent
    {
        IPAddress = "10.0.0.4",
    };
    ArmOperation<BackendAddressInboundNatRulePortMappings> lro = await backendAddressPool.GetInboundNatRulePortMappingsLoadBalancerAsync(WaitUntil.Completed, content);
    BackendAddressInboundNatRulePortMappings result = lro.Value;
    
    Console.WriteLine($"Succeeded: {result}");
    

    For other language examples, please refer: https://free.blessedness.top/en-us/rest/api/load-balancer/load-balancers/list-inbound-nat-rule-port-mappings?view=rest-load-balancer-2024-05-01&tabs=dotnet#query-inbound-nat-rule-port-mapping

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.