Hello Neha
Welcome to Microsoft Q&A Platform, thanks for posting your query here.
Yes, you can use the Azure Instance Metadata Service (IMDS) to retrieve metadata for your container instance. 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.
To access IMDS, you can use the following sample code:
curl -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2021-02-01"
This will return a JSON object containing metadata about the instance, including the subscription ID, resource group name, and container group name.
You can then parse the JSON object to extract the information you need. Here's an example of how to extract the subscription ID:
curl -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2021-02-01" | jq -r .compute.subscriptionId'
This will return the subscription ID as a string.
I hope this information helps you.