[Azure] How can I retrieve the private IP addresses of all instances within an Azure Virtual Machine Scale Set using the Instance Metadata Service (IMDS)?

Rushikesh Gaidhani 161 Reputation points
2024-11-26T01:04:01.57+00:00

I am utilizing the IMDS endpoint mentioned to retrieve the private IP address of a specific scale set instance.
(Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -Uri "http://169.254.169.254/metadata/instance/network/interface?api-version=2021-02-01").ipv4.ipAddress.privateIpAddress

However, is there a method to obtain the private IP addresses of all instances within an Azure VMSS using IMDS?"

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.
417 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Mounika Reddy Anumandla 980 Reputation points Microsoft Vendor
    2024-11-26T06:09:32.92+00:00

    Hi Rushikesh Gaidhani,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    IMDS is available for running instances of virtual machines (VMs) and scale set instances. All endpoints support VMs created and managed by using Azure Resource Manager. IMDS is a REST API that's available at a well-known, non-routable IP address (169.254.169.254). You can only access it from within the VM. Communication between the VM and IMDS never leaves the host.

    However, I do not think IMDS provides a direct method to retrieve the private IP addresses of all instances within a VMSS. Instead, you can use Azure CLI or PowerShell scripts to query the network interfaces associated with the VMSS instances and retrieve their private IP addresses. The IMDS only provides metadata for the instance making the request, so if you want to gather information about all instances, you'll need to either loop through each instance or use Azure CLI.https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service?tabs=windows

    Here's a PowerShell script that can help you retrieve the private IP addresses of all instances within a VMSS: https://learn.microsoft.com/en-us/answers/questions/1300833/how-to-retrieve-private-ip-addresses-and-names-of

    If you have any further queries, do let us know.

    If the answer is helpful, please click "Accept Answer" and "Upvote it."


Your answer

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