Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Analyze and reduce Azure cloud costs by right-sizing resources, reservations, and spending policies
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
cost-optimization/azure-resource-graph.md
1# Azure Resource Graph Queries for Cost Optimization23Azure Resource Graph (ARG) enables fast, cross-subscription resource querying using KQL via `az graph query`. Use it to find orphaned resources, unused infrastructure, and optimization targets.45## How to Query67Use the `extension_cli_generate` MCP tool to generate `az graph query` commands:89```yaml10azure__extension_cli_generate11intent: "query Azure Resource Graph to <describe what you want to find>"12cli-type: "az"13```1415Or construct directly:1617```bash18az graph query -q "<KQL>" --query "data[].{name:name, type:type}" -o table19```2021> ⚠️ **Prerequisite:** `az extension add --name resource-graph`2223## Key Tables2425| Table | Contains |26|-------|----------|27| `Resources` | All ARM resources (name, type, location, properties, tags) |28| `ResourceContainers` | Subscriptions, resource groups, management groups |29| `AdvisorResources` | Cost and performance recommendations |3031## Cost Optimization Query Patterns3233**Find orphaned (unattached) managed disks:**3435```kql36Resources37| where type =~ 'microsoft.compute/disks'38| where isempty(managedBy)39| project name, resourceGroup, location, diskSizeGb=properties.diskSizeGB, sku=sku.name40```4142**Find unattached public IP addresses:**4344```kql45Resources46| where type =~ 'microsoft.network/publicipaddresses'47| where isempty(properties.ipConfiguration)48| project name, resourceGroup, location, sku=sku.name49```5051**Find orphaned network interfaces:**5253```kql54Resources55| where type =~ 'microsoft.network/networkinterfaces'56| where isempty(properties.virtualMachine)57| project name, resourceGroup, location58```5960**Resource count by SKU/tier (spot oversized resources):**6162```kql63Resources64| where isnotempty(sku.name)65| summarize count() by type, tostring(sku.name)66| order by count_ desc67```6869**Tag coverage for cost allocation:**7071```kql72Resources73| extend hasCostCenter = isnotnull(tags['CostCenter'])74| summarize total=count(), tagged=countif(hasCostCenter) by type75| extend coverage=round(100.0 * tagged / total, 1)76| order by total desc77```7879**Find idle load balancers (no backend pools):**8081```kql82Resources83| where type =~ 'microsoft.network/loadbalancers'84| where array_length(properties.backendAddressPools) == 085| project name, resourceGroup, location, sku=sku.name86```8788**Get Advisor cost recommendations:**8990```kql91AdvisorResources92| where properties.category == 'Cost'93| project name, impact=properties.impact, description=properties.shortDescription.solution94```9596## Tips9798- Use `=~` for case-insensitive type matching (resource types are lowercase)99- Navigate properties with `properties.fieldName`100- Use `--first N` to limit result count101- Use `--subscriptions` to scope to specific subscriptions102- Cross-reference orphaned resources with cost data from Cost Management API103