1

I want to deploy a scale set in two different regions on Azure from an custom virtual machine image. I've worked out how to do that, but I would like to switch configuration information depending on the region that the scale set is deployed. How can I detect the region of a virtual machine from that virtual machine?

The virtual machine is running CentOS.

  • 1
    where exactly must this happen? – 4c74356b41 Sep 13 '18 at 6:04
  • 1
    On the virtual machine. I want a configuration file updated based on the region. The configuration file gets updated just before the web server starts up. – Anonymous1 Sep 13 '18 at 6:08
  • 1
    how do you configure it? with azure native capabilities or? – 4c74356b41 Sep 13 '18 at 6:12
  • 1
    it's configured with a custom script in /etc – Anonymous1 Sep 13 '18 at 6:18
  • 1
    Perhaps this could be done with the Azure CLI and the az vm list command which returns a location for each VM. Might not be advisable to run this from the actual VM though for the sake of obtaining a single variable. – v25 Sep 18 '18 at 18:47
1

There is a nice easy way to do this, use the metadata service inside the VM. You can run this command from inside the VM (URL is the same for all VMs)

curl -H @{'Metadata'='true'} http://169.254.169.254/metadata/instance?api-version=2017-03-01

and it will return a json object with data about the VM, including the region:

{
  "compute": {
    "location": "westus",
    "name": "avset2",
    "offer": "UbuntuServer",
    "osType": "Linux",
    "placementGroupId": "",
    "platformFaultDomain": "1",
    "platformUpdateDomain": "1",
    "publisher": "Canonical",
    "resourceGroupName": "myrg",
    "sku": "16.04-LTS",
    "subscriptionId": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
    "tags": "",
    "version": "16.04.201708030",
    "vmId": "13f56399-bd52-4150-9748-7190aae1ff21",
    "vmScaleSetName": "",
    "vmSize": "Standard_D1",
    "zone": "1"
  },
  "network": {
    "interface": [
      {
        "ipv4": {
          "ipAddress": [
            {
              "privateIpAddress": "10.1.2.5",
              "publicIpAddress": "X.X.X.X"
            }
          ],
          "subnet": [
            {
              "address": "10.1.2.0",
              "prefix": "24"
            }
          ]
        },
        "ipv6": {
          "ipAddress": []
        },
        "macAddress": "000D3A36DDED"
      }
    ]
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.