3

Is there a way to get the subscription id from the running (LINUX)VM instance in AZURE?

Can WALinuxAgent read the subscription ID from the internal server ?

Gauri B
  • 41
  • 1
  • 3

2 Answers2

7

This can be achieved using the Azure Instance Metadata Service. Calling this service from your VM will return a JSON with SubscriptionId among other useful data. Sample Microsoft bash script for calling the metadata service (with updated version in the request):

sudo apt-get install curl
sudo apt-get install jq
curl -H Metadata:True "http://169.254.169.254/metadata/instance?api-version=2017-08-01&format=json" | jq .

See "Response" section in provided link for sample response, with subscriptionId.

moonrobin
  • 213
  • 2
  • 9
0

You can use powershell to achieve this. First of all. What kind of VM deployment model?

ARM

In this case it very simple.

$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
$vm.Id

You'll see - "/subscriptions/{subscriptionId}/..."

Classic

If you know resource group VM was deployed to, use following:

$resource = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.ClassicCompute/virtualMachines -Name $vmName
$resource.ResourceId

Same - you"ll see "/subscriptions/{subscriptionId}/..."

Way to find resourceGroupName, if unknown (in case you write some automative script):

$vm = Get-AzureVM | Where {$_.Name -eq $vmName}
$service = Get-AzureService -ServiceName $vm.ServiceName
$service.ExtendedProperties.ResourceGroup

Hope it helps

Alexander S.
  • 844
  • 1
  • 13
  • 29
  • 1
    What is the Linux equivalent of this since Powershell would not be available in Linux? Azure CLI requires login which defeats the purpose of automation – rajeshnair Dec 30 '16 at 09:27