Getting Started with the Azure Resource Graph
The Azure Resource Graph is a very useful service which allows you to query your Azure resources from the Resource Graph Explorer within the Azure Portal, using Kusto Query Langauge (KQL).
I recently used it on a project where we needed to inspect a complex Azure environment with many different Virtual Machines and Disks. In this blog post I will help you get started with the Resource Graph and share some very basic KQL queries!
Prerequisites
- Access to an Azure subscription
If you do not have your own Azure subscription, you can always follow along with an Azure sandbox. Activate a sandbox and deploy a virtual machine using the Exercise - Create a VM using the Azure portal on Microsoft Learn.
Getting Started
From the Azure Portal navigate to the Resource Graph Explorer or follow this link.
Quering is simple, and you can view all resources by selecting all the data from the resources
table. If you are familiar with SQL, check out this SQL to Kusto cheat sheet.
resources
Notice that this brings back all default columns. You can limit the columns in your selection by using the project
keyword. I will also order the results by including an order by
clause.
resources
| project name, resourceGroup, location
| order by resourceGroup asc
We can filter our results by using a where
statement. We need to filter first, because you cannot apply a filter on a column not included in the projection. First we will filter on a specific resource group.
resources
| where resourceGroup == "<YOUR RESOURCE GROUP>"
| project name, location
We can also include all resource specific attributes by including the properties
column. Notice that this brings back all properties as JSON. We want to look at Virtual Machines, so filter on type
.
resources
| where type == "microsoft.compute/virtualmachines"
| project name, location, resourceGroup, properties
You can even access attributes within the properties JSON, like we do here with vmSize
.
resources
| where type == "microsoft.compute/virtualmachines"
| project name, location, resourceGroup, vmSize=properties.hardwareProfile.vmSize
Conclusion
As you can see, the Azure Resource Graph is a powerful way to query resources. With support for many common operators, aggregate functions, table joins and geospatial visualizations you can easily create powerful queries to gain insights and vizualize the results.
Useful links
- Azure Resource Graph
- Kusto Query Langauge