How to programmatically find out the current region in the azure role?

I need to programmatically find the current region (for example, "Western USA" or "Eastern America") where my current role is running. Is there an API to find this?

+6
source share
3 answers

You can get this information only if you use Management Api.

Either REST, or you can use C # Windows Azure Management Libraries (Prerelease on nuget).

But note that you need to configure management certificates for information.

A simpler alternative is to create a parameter in your cloud service and set values ​​when creating the deployment configuration. I do this and have a deployment configuration for the regions I am targeting.

using( var azure = CloudContext.Clients.CreateComputeManagementClient(...)) { var service = await azure.HostedServices.GetDetailedAsync("servicename"); // service.Properties.Location // service.Properties.AffinityGroup; } using(var azure = CloudContext.Clients.CreateManagementClient(...)) { var affinityGroup = await azure.AffinityGroups.GetAsync("name",new CancellationToken()); // affinityGroup.Location } 

Here ... these are credentials, or a management certificate, or your WAAD tokens. (ADAL: Active Directory Authentication Library) can be used for tokens.

here is the code to get the credentials from the certificate:

  public static CertificateCloudCredentials GetCertificateCloudCredentials( string certificateThumbprint, string subscriptionId) { var certificate = CertificateHelper.LoadCertificate( StoreName.My, StoreLocation.LocalMachine, certificateThumbprint); if (certificate == null) throw new Exception( string.Format("Certificate with thumbprint '{0}' not found", certificateThumbprint)); var cred = new CertificateCloudCredentials( subscriptionId, certificate ); return cred; } 
+2
source

Consider using Get a Cloud Service in the Service Management API. When you provide a service of which your roles are part, you may receive a response similar to the following. Pay attention to the location field that I took.

 <?xml version="1.0" encoding="utf-8"?> <HostedService xmlns="http://schemas.microsoft.com/windowsazure"> <Url>hosted-service-url</Url> <ServiceName>hosted-service-name</ServiceName> <HostedServiceProperties> <Description>description</Description> <AffinityGroup>name-of-affinity-group</AffinityGroup> **<Location>location-of-service</Location >** <Label>base-64-encoded-name-of-service</Label> <Status>current-status-of-service</Status> <DateCreated>creation-date-of-service</DateCreated> <DateLastModified>last-modification-date-of-service</DateLastModified> <ExtendedProperties> <ExtendedProperty> <Name>name-of-property</Name> <Value>value-of-property</Value> </ExtendedProperty> </ExtendedProperties> <GuestAgentType>type-of-guest-agent</GuestAgentType> </HostedServiceProperties> <DefaultWinRmCertificateThumbprint>thumbprint-of-winrm-certificate</DefaultWinRmCertificateThumbprint> </HostedService> 
+2
source

This information is available in the Azure Instance Metadata Service (IMDS) . The REST endpoint for any virtual machine running in a public Azure cloud is http://169.254.169.254/metadata/instance?api-version=2017-04-02 . A metadata object contains two subobjects, one for "computing" and one for "network." The name of the region is displayed in the "location" element of the "compute" object.

Sample code in several languages ​​for accessing various IMDS data items is available in the Microsoft / azureimds repository on github . Much more information than I show here is available through the IMDS API version 2018-10-01; see IMDS docs for details.

 $ curl -s -H Metadata:True "http://169.254.169.254/metadata/instance?api-version=2017-04-02&format=json" | jq . { "compute": { "location": "westus2", "name": "samplevm", "offer": "UbuntuServer", "osType": "Linux", "platformFaultDomain": "0", "platformUpdateDomain": "0", "publisher": "Canonical", "sku": "18.04-LTS", "version": "18.04.201904020", "vmId": "(redacted)", "vmSize": "Standard_D2s_v3" }, "network": { "interface": [ { "ipv4": { "ipAddress": [ { "privateIpAddress": "10.0.0.7", "publicIpAddress": "" } ], "subnet": [ { "address": "10.0.0.0", "prefix": "24" } ] }, "ipv6": { "ipAddress": [] }, "macAddress": "(redacted)" } ] } } 
0
source

All Articles