question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load balancer creation/management sample code?

See original GitHub issue

Library name and version

Azure.ResourceManager.Network 1.0.0-beta.7

Query/Question

I’ve been struggling with creating a load balancer using the Azure.ResourceManager.Network 1.0.0-beta.7 SDK and haven’t found any SDK specific sample code or examples.

Question 1: Is there any sample code for managing load balancers with the .NET Azure ARM SDK?

I’ve done a lot of Googling and haven’t found anything but samples for the Azure CLI, PowerShell, and older SDKs. I did find a couple samples here but these didn’t cover load balancers.

Question 2: What’s the process for adding a NIC to a backend address pool?

I’ve tried the pseudo code below but I’m unable to initialize a LoadBalancerBackendAddress because setting the NetworkInterfaceIPConfigurationId property throws a NullReferenceException. This happens because the default LoadBalancerBackendAddress() constructor doesn’t initialize the internal NetworkInterfaceIPConfiguration property.

var azure                   = new ArmClient(new DefaultAzureCredential());
var location                = new AzureLocation("centralus");
var resourceGroupCollection = azure.GetResourceGroupCollection();
var resourceGroup           = (await resourceGroupCollection.GetAsync("my-resources")).Value;
var loadBalancerCollection  = resourceGroup.GetLoadBalancers();
var publicAddress           = /* set to an existing public IP address resource */
var nic                     = /* set to the primary (and only) NIC resource for a deployed VM */

// Create a load balancer with a single frontend and and an empty backend.

var loadBalancerData = 
    new LoadBalancerData()
    {
        Location = location,
        Sku      = new LoadBalancerSku() { Name = "Standard", Tier = "Regional" }
    };

loadBalancerData.FrontendIPConfigurations.Add(
    new FrontendIPConfigurationData()
    {
        Name            = "my-frontend",
        PublicIPAddress = publicAddress.Data,
    });

loadBalancerData.BackendAddressPools.Clear();
loadBalancerData.BackendAddressPools.Add(
    new BackendAddressPoolData()
    {
         Name = "my-backend"
    });

loadBalancer = (await loadBalancerCollection.CreateOrUpdateAsync(WaitUntil.Completed, "my-loadbalancer", WithNetworkTags(loadBalancerData))).Value;

// Add a NIC to the backend and update the load balancer.

var nicIpConfigId = nic.GetNetworkInterfaceIPConfigurations().First().Id;

loadBalancer.Data.BackendAddressPools.First().Add(
    new LoadBalancerBackendAddress()
    {
        Name                              = "my-address",
        NetworkInterfaceIPConfigurationId = nicIpConfigId  // <--- Throws a NullReferenceException because the R/O backing property is NULL
    });
    
loadBalancer = (await loadBalancerCollection.CreateOrUpdate(WaitUntil.Completed, loadBalancer.Name, loadBalancer.Data)).Value;

For fun, I tried using reflection to initialize the internal NetworkInterfaceIPConfiguration property’s backing property to a new WritableSubResource. This fixed the nullref exception, but the load balancer’s backend address pool was still empty after the update.

Environment

.NET SDK (reflecting any global.json): Version: 6.0.300 Commit: 8473146e7d

Runtime Environment: OS Name: Windows OS Version: 10.0.19044 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\6.0.300\

Host (useful for support): Version: 6.0.5 Commit: 70ae3df4a6

.NET SDKs installed: 3.1.419 [C:\Program Files\dotnet\sdk] 5.0.407 [C:\Program Files\dotnet\sdk] 5.0.408 [C:\Program Files\dotnet\sdk] 6.0.101 [C:\Program Files\dotnet\sdk] 6.0.105 [C:\Program Files\dotnet\sdk] 6.0.300 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 3.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jefflillcommented, May 17, 2022

I figured this out: you need to add a NIC to a backend address pool from the NIC side, something like:

var resourceGroup           = (await resourceGroupCollection.GetAsync("my-resources")).Value;
var loadBalancerCollection  = resourceGroup.GetLoadBalancers();
var nicCollection           = resourceGroup.GetNetworkInterfaces();
var nic                     = (await nicCollection.GetAsync("my-nic")).Value;

// Create a load balancer with a single frontend and and an empty backend.

var loadBalancerData =
    new LoadBalancerData()
    {
        Location = location,
        Sku = new LoadBalancerSku() { Name = "Standard", Tier = "Regional" }
    };

loadBalancerData.FrontendIPConfigurations.Add(
    new FrontendIPConfigurationData()
    {
        Name            = "my-frontend",
        PublicIPAddress = publicAddress.Data,
    });

loadBalancerData.BackendAddressPools.Clear();
loadBalancerData.BackendAddressPools.Add(
    new BackendAddressPoolData()
    {
        Name = "my-backend"
    });

loadBalancer = (await loadBalancerCollection.CreateOrUpdateAsync(WaitUntil.Completed, "my-loadbalancer", WithNetworkTags(loadBalancerData))).Value;

// Add an existing NIC to the load balancer's backend address pool.

var ipConfiguration = nic.Data.IPConfigurations.First();

ipConfiguration.LoadBalancerBackendAddressPools.Add(
    new BackendAddressPoolData()
    {
        Id = loadBalancerData.BackendAddressPools.Single(pool => pool.Name == "my-backend").Data.Id
    });

nic = (await nicCollection.CreateOrUpdateAsync(WaitUntil.Completed, "my-nic", azureVm.Nic.Data)).Value;

What threw me here, was that the BackendAddressPoolData.LoadBalancerBackendAddresses property is a read/write IList instead of a IReadOnlyList. This led me to believe that I could manage the NIC references here. I suppose this is R/W so that developers can add static IP addresses rather then adding NICs.

So, I guess this issue has turned into a request for load balancer sample code.

0reactions
ArthurMa1978commented, May 18, 2022

@jefflill thank you for reaching us, and provide this useful code, we will have a sampe for this scenario. @fengzhou-msft will follow up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create an Application Load Balancer - AWS Documentation
To create a load balancer using the AWS Management Console, complete the following tasks. Tasks. Step 1: Configure a target group; Step 2:...
Read more >
Quickstart: Create a public load balancer - ARM template
This quickstart shows how to create a load balancer by using an Azure Resource Manager template.
Read more >
Setting up an external Application Load Balancer
Connect your domain to your load balancer. After the load balancer is created, note the IP address that is associated with the load...
Read more >
Creating a Load Balancer
Create a load balancer to provide automated traffic distribution from one entry point to multiple servers reachable from your virtual cloud network (VCN)....
Read more >
Create an Application Load Balancer (ALB) in AWS - YouTube
In this hands-on tutorial, I'll walk you through how set up two new ... Creating an Application Load Balancer in AWS 06:54 –...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found