如何无需指定Location参数获取Azure全区域VM Sizes列表?
Great question! Unfortunately, there isn't a single Azure Compute API endpoint that lets you fetch VM sizes across all regions without specifying a location parameter directly. The official vmSizes API you referenced requires a location to return sizes specific to that region—this is because VM size availability can vary significantly by region.
But don't worry, there are two practical workarounds to gather VM sizes for every Azure region:
1. Iterate through all regions via the Azure API
You can first pull the full list of Azure regions, then loop through each one to call the vmSizes API and aggregate the results:
- Step 1: Fetch all available regions using the Locations API:
GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Compute/locations?api-version=2023-07-01 - Step 2: For each
namevalue returned in the response, call thevmSizesAPI for that region:GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Compute/locations/{location}/vmSizes?api-version=2017-12-01 - Step 3: Combine the results from all regions into a single dataset.
Note: Keep Azure API rate limits in mind when doing this. You may need to add small delays between requests or handle 429 (rate limit exceeded) responses with retry logic.
2. Use Azure CLI or PowerShell to automate the process
Azure's command-line tools take care of the region iteration for you, making this a simpler approach:
Azure CLI
Run this command to fetch VM sizes for all regions:
az account list-locations --query "[].name" -o tsv | xargs -I {} az vm list-sizes --location {}
Azure PowerShell
Use this script to retrieve and output VM sizes across every region:
# Get all Azure regions $locations = Get-AzLocation | Select-Object -ExpandProperty Location # Loop through each region and fetch VM sizes foreach ($location in $locations) { Write-Host "Fetching VM sizes for region: $location" Get-AzVMSize -Location $location }
Both tools automatically handle pagination and basic rate limiting, so you don't have to manage those details manually.
内容的提问来源于stack exchange,提问作者Neel

