DEV Community

Larry Claman
Larry Claman

Posted on • Originally published at larryclaman.github.io on

Querying for Azure Regional Pairs

A teammate recently posted a question on our Team's channel, asking how to programmatically query to find out an Azure Region's pair. Easy I thought - except it turns out, this isn't exposed in the standard PowerShell commands, nor in the Azure CLI.

The PowerShell command Get-AzLocation only returns the Location, DisplayName, and Providers:

PS C:\> Get-AzLocation|ft

Location           DisplayName          Providers
--------           -----------          ---------
eastasia           East Asia            {Microsoft.Media, Microsoft.HDInsight, Microsoft.SqlVirtualMachine, Microsoft.DevOps...}
southeastasia      Southeast Asia       {Microsoft.Media, Microsoft.HDInsight, Microsoft.DataShare, Microsoft.SqlVirtualMachine...}
centralus          Central US           {Microsoft.Media, Microsoft.HDInsight, Microsoft.SqlVirtualMachine, Microsoft.DevOps...}
eastus             East US              {Microsoft.Media, Microsoft.HDInsight, Microsoft.DataShare, Microsoft.SqlVirtualMachine...}
[snip]

Meanwhile, the Azure CLI returns more info, but not the pair:

PS C:\> az account list-locations
DisplayName           Latitude    Longitude    Name
--------------------  ----------  -----------  ------------------
East Asia             22.267      114.188      eastasia
Southeast Asia        1.283       103.833      southeastasia
Central US            41.5908     -93.6208     centralus
East US               37.3719     -79.8164     eastus
East US 2             36.6681     -78.3889     eastus2
West US               37.783      -122.417     westus
[snip]

All hope is not lost! You can find the region's pair, but you need to query the Azure REST api directly, as documented at https://docs.microsoft.com/en-us/rest/api/resources/Subscriptions/ListLocations. In the past, you'd need to fire up your favorite REST client (eg, armclient, Vscode + rest client extension, Postman, etc), but in exploring this, I learned an easier way: You can now make Azure REST calls directly from the CLI! Here's what that looks like: (Replace {yoursubid} with your specific subscription id)

az rest --method GET --uri https://management.azure.com/subscriptions/{yoursubid}/locations?api-version=2020-01-01 --output json
{
  "value": [
    {
      "displayName": "East US",
      "id": "/subscriptions/{yoursubid}/locations/eastus",
      "metadata": {
        "geographyGroup": "US",
        "latitude": "37.3719",
        "longitude": "-79.8164",
        "pairedRegion": [
          {
            "id": "/subscriptions/{yoursubid}/locations/westus",
            "name": "westus"
          }
        ],
        "physicalLocation": "Virginia",
        "regionCategory": "Recommended",
        "regionType": "Physical"
      },
      "name": "eastus",
      "regionalDisplayName": "(US) East US"
    },
[snip]

What's really cool is you can use the CLI's built-in jmespath query engine to filter and massage the results, eg: (Column1 is the region, and Column2 is the pair)

az rest --method GET --uri https://management.azure.com/subscriptions/{yoursubid}/locations?api-version=2020-01-01 --output table --query 'value[].[name,metadata.pairedRegion[].name]'

Column1              Column2
-------------------  ----------------------
eastus               ['westus']
eastus2              ['centralus']
southcentralus       ['northcentralus']
westus2              ['westcentralus']
australiaeast        ['australiasoutheast']
[snip]

(I'm still working on my jmespath ninja skills, so there's still a little cleanup to be done on this table, but it gets the point across.)

Top comments (0)