Hey @pipi ,
In VMSS, network interfaces are internal resources managed by the VMSS resource and are not directly visible in the resource list thus you got the resourceNotFound error. I'm sharing 2 codes generated, can you try both of them and let me know which one works out for you.
1st code, has the ideal way to gather details, ie list out VMSS and it's resources and then get it.
2nd code has the way to list out all NICs in the resource group and then find out the nic based on the VM/VMSS name and share extract the details. Let me know which one works out for you.
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.Network;
using System;
using System.Linq;
public async Task<string> GetVmssInstancePrivateIpAsync(
string subscriptionId,
string resourceGroupName,
string vmssName,
string instanceId)
{
var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential, subscriptionId);
try
{
// Get the VMSS instance
var vmssResourceId = VirtualMachineScaleSetResource.CreateResourceIdentifier(
subscriptionId,
resourceGroupName,
vmssName);
var vmss = armClient.GetVirtualMachineScaleSetResource(vmssResourceId);
var vmInstance = await vmss.GetVirtualMachineScaleSetVmAsync(instanceId);
var vmInstanceResource = vmInstance.Value;
// Get the network profile and primary network interface
var networkProfile = vmInstanceResource.Data.NetworkProfile;
if (networkProfile?.NetworkInterfaces?.Count == 0)
{
throw new Exception("No network interfaces found for this VM instance.");
}
// Find the primary network interface (or take the first one)
var primaryNicReference = networkProfile.NetworkInterfaces
.FirstOrDefault(nic => nic.Primary == true) ??
networkProfile.NetworkInterfaces.First();
// Extract the NIC name from the resource ID
// The resource ID format is: /subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.Network/networkInterfaces/{nicName}
var nicResourceId = primaryNicReference.Id;
var nicName = nicResourceId.Name;
// Get the network interface resource
var nicResource = armClient.GetNetworkInterfaceResource(nicResourceId);
var networkInterface = await nicResource.GetAsync();
// Get the primary private IP address
var primaryIpConfig = networkInterface.Value.Data.IPConfigurations
.FirstOrDefault(ip => ip.Primary == true) ??
networkInterface.Value.Data.IPConfigurations.FirstOrDefault();
if (primaryIpConfig == null || string.IsNullOrEmpty(primaryIpConfig.PrivateIPAddress))
{
throw new Exception("No private IP address found for the network interface.");
}
return primaryIpConfig.PrivateIPAddress;
}
catch (Exception ex)
{
// Enhanced error handling
throw new Exception($"Error retrieving private IP: {ex.Message}. " +
$"Make sure the VMSS instance '{instanceId}' exists and is running, " +
$"and that your managed identity has 'Network Contributor' or 'Reader' " +
$"permissions on the resource group '{resourceGroupName}'.", ex);
}
}
If above code throws error then only try below code:
public async Task<string> GetVmssInstancePrivateIpAlternativeAsync(
string subscriptionId,
string resourceGroupName,
string vmssName,
string instanceId)
{
var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential, subscriptionId);
// VMSS network interfaces follow a pattern: {vmssName}nic{instanceIndex}
// For example: "myvmssnic001", "myvmssnic002", etc.
string expectedNicName = $"{vmssName}nic{instanceId.PadLeft(3, '0')}";
try
{
// Get the resource group
var subscription = await armClient.GetDefaultSubscriptionAsync();
var resourceGroup = await subscription.GetResourceGroupAsync(resourceGroupName);
// Get all network interfaces in the resource group
var networkInterfaces = await resourceGroup.Value.GetNetworkInterfaces()
.GetAllAsync()
.ToEnumerableAsync();
// Find the NIC for this VMSS instance
var targetNic = networkInterfaces.FirstOrDefault(nic =>
nic.Data.Name.StartsWith($"{vmssName}nic", StringComparison.OrdinalIgnoreCase) &&
nic.Data.VirtualMachine?.Id.EndsWith($"/virtualMachines/{instanceId}") == true);
if (targetNic == null)
{
// Try alternative naming pattern
targetNic = networkInterfaces.FirstOrDefault(nic =>
nic.Data.Name.Contains(instanceId) &&
nic.Data.Name.Contains(vmssName, StringComparison.OrdinalIgnoreCase));
}
if (targetNic == null)
{
throw new Exception($"Network interface for VMSS instance '{instanceId}' not found. " +
$"Looked for patterns: '{vmssName}nic*' containing instance ID '{instanceId}'");
}
// Get the primary private IP
var primaryIpConfig = targetNic.Data.IPConfigurations
.FirstOrDefault(ip => ip.Primary == true) ??
targetNic.Data.IPConfigurations.FirstOrDefault();
return primaryIpConfig?.PrivateIPAddress ??
throw new Exception("No private IP address found on the network interface.");
}
catch (Exception ex)
{
throw new Exception($"Failed to get private IP: {ex.Message}", ex);
}
}