Automating the extraction of Azure Regions resiliency information

Azure paired regions and zone compatible regions are fundamental for ensuring redundancy and high availability of your applications in the cloud. Understanding the geography, physical regions, and their pairings can help you make informed decisions when architecting solutions on Azure. In this guide, we'll walk through how you can automate the extraction of Azure regions information using PowerShell and Azure CLI and format them as you like.

Prerequisites

  • Azure CLI installed on your machine. You can download it from here.
  • PowerShell installed on your machine. You can download it from here.
  • You need to be logged in to your Azure account using az login.

PowerShell Script to Extract Azure Paired Regions

Below is a PowerShell script that uses Azure CLI to list all the regions, filters them to get physical regions, and formats the output to display the geography group, geography, region name, and paired region.

# Retrieve all Azure regions
$results = az account list-locations | ConvertFrom-Json

# Filter only physical regions
$physicalResults = $results | Where-Object { $_.metadata.regionType -eq 'Physical' }

# Select and format the desired properties
$formattedResults = $physicalResults | Select-Object `
    @{name="GeographyGroup"; Expression = {$_.metadata.geographyGroup}}, `
    @{name="Geography"; Expression = {$_.metadata.geography}}, `
    @{name="Region"; Expression = {$_.name}}, `
    @{name="PairedRegion"; Expression = {$_.metadata.pairedRegion[0].name}}

# Convert to JSON format with depth for nested objects
$jsonOutput = $formattedResults | ConvertTo-Json -Depth 5

# Output the JSON result
$jsonOutput

Explanation

  • Retrieve Regions: The command az account list-locations retrieves all the Azure regions available for your subscription. We use ConvertFrom-Json to convert the output into a PowerShell object for easier manipulation.
  • Filter Physical Regions: We filter the regions to include only those with a regionType of 'Physical'.
  • Select and Format Properties: We use Select-Object to extract specific properties and rename them for clarity. This includes the geography group, geography, region name, and the name of the paired region.
  • Convert to JSON: Finally, we convert the filtered and formatted data back into JSON format using ConvertTo-Json, allowing for easy consumption in other tools or systems.

Using that kind of information, we can automate and ease all type of decisions. Here is an example where we could use that strategy to produce Azure Resource Graph statements and augment queries with resiliency information.

$results = az account list-locations | ConvertFrom-Json

$physicalResults = $results | Where-Object { $_.metadata.regionType -eq 'Physical' }

$results = $physicalResults `
    | Select-Object @{ name = "GeographyGroup"; Expression = { $_.metadata.geographyGroup } }, `
        @{ name = "Geography"; Expression = { $_.metadata.geography } }, `
        @{ name = "region"; Expression = { $_.name } }, `
        @{ name = "paired_region"; Expression = { $_.metadata.pairedRegion[0].name } }, `
        @{ name = "zones"; Expression = { $_.availabilityZoneMappings } } `
    | Where-Object GeographyGroup -in ('US', 'Canada')


# extract regions that have a paired region
$PairedRegions = $results `
    | Where-Object paired_region -ne $null `
    | Sort-Object Region `
    | Select-Object -ExpandProperty region

$joinedStringValues = "'$($PairedRegions -join "','")'"
# Produce a ARG statement for identifying resources in paired regions
"| extend drp_in_paired_region = case(location in ($joinedStringValues), true, false)"


# extract regions that have a zones support
$RegionsWithZones = $results `
    | Where-Object zones -ne $null `
    | Sort-Object Region `
    | Select-Object -ExpandProperty region    

$joinedStringValues = "'$($RegionsWithZones -join "','")'"
# Produce a ARG statement for identifying resources in regions with zones support
"| extend drp_region_support_az = case(location in ($joinedStringValues), true, false)"    

This script will produce the following outputs:

| extend drp_in_paired_region = case(location in ('canadacentral','canadaeast','centralus','centraluseuap','eastus','eastus2','eastus2euap','eastusstg','northcentralus','southcentralus','southcentralusstg','westcentralus','westus','westus2','westus3'), true, false)

| extend drp_region_support_az = case(location in ('canadacentral','centralus','eastus','eastus2','eastus2euap','southcentralus','westus2','westus3'), true, false)

Conclusion

This provides an automated way to extract and use Azure's regional resiliency information, which can be crucial for planning and deploying resilient and high-available applications. By leveraging Azure CLI and PowerShell, you can easily integrate this process into your existing automation workflows.

References

Feel free to modify the script to suit your specific needs, such as outputting the results to a file or integrating with other systems like Azure Resource Graph (ARG) for further processing. Happy automating!