Quickstart: Create Resource Graph shared query using ARM template
In this quickstart, you use an Azure Resource Manager template (ARM template) to create a Resource Graph shared query. Resource Graph queries can be saved as a private query or a shared query. A private query is saved to the individuals portal profile and isn't visible to others. A shared query is a Resource Manager object that can be shared with others through permissions and role-based access. A shared query provides common and consistent execution of resource discovery.
An Azure Resource Manager template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. The template uses declarative syntax. You describe your intended deployment without writing the sequence of programming commands to create the deployment.
If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. The template opens in the Azure portal.
If you don't have an Azure subscription, create a trial subscription account before you begin.
In this quickstart, you create a shared query called Count VMs by OS. To try this query in SDK or in portal with Resource Graph Explorer, go to Samples - Count virtual machines by OS type.
The template used in this quickstart is from Azure Quickstart Templates.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.6.1.6515",
"templateHash": "3085789505121407565"
}
},
"parameters": {
"queryName": {
"type": "string",
"defaultValue": "Count VMs by OS",
"metadata": {
"description": "The name of the shared query."
}
},
"queryCode": {
"type": "string",
"defaultValue": "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)",
"metadata": {
"description": "The Azure Resource Graph query to be saved to the shared query."
}
},
"queryDescription": {
"type": "string",
"defaultValue": "This shared query counts all virtual machine resources and summarizes by the OS type.",
"metadata": {
"description": "The description of the saved Azure Resource Graph query."
}
}
},
"resources": [
{
"type": "Microsoft.ResourceGraph/queries",
"apiVersion": "2018-09-01-preview",
"name": "[parameters('queryName')]",
"location": "global",
"properties": {
"query": "[parameters('queryCode')]",
"description": "[parameters('queryDescription')]"
}
}
]
}
The resource defined in the template is Microsoft.ResourceGraph/queries
Select the following image to sign in to the Azure portal and open the template:
Select or enter the following values:
Name Value Subscription Select your Azure subscription. Resource group Select Create new, specify a name, and then select OK. Or select an existing resource group. Location Select a region. For example, China North. Query name Use the default value: Count VMs by OS. Query code Use the default value: Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)
Query description Use the default value: This shared query counts all virtual machine resources and summarizes by the OS type. Select Review + create.
Select Create.
Some other resources:
To find more sample templates, see Browse code samples.
To learn how to develop ARM templates, see Azure Resource Manager documentation.
To learn subscription-level deployment, see Create resource groups and resources at the subscription level.
To run the new shared query, follow these steps:
- From the portal search bar, search for Resource Graph queries and select it.
- Select the shared query named Count VMs by OS, then select the Results tab on the Overview page.
The shared query can also be opened from Resource Graph Explorer:
- From the portal search bar, search for Resource Graph Explorer and select it.
- Select the Open a query.
- Change Type to Shared queries. If you don't see the Count VMs by OS in the list, use the filter box to limit the results by Subscription or Resource group. When the Count VMs by OS shared query is visible, select its name.
- Select Run query and results are displayed in the Results tab.
In Resource Graph Explorer, on left side of page, you can change the Scope to use Directory, Management group, or Subscription.
To remove the shared query created, follow these steps:
- From the portal search bar, search for Resource Graph queries and select it.
- Set the check box next to the shared query named Count VMs by OS.
- Select Delete.
- Enter delete and select Delete.
- Select Delete from the confirmation prompt.
In this quickstart, you created a Resource Graph shared query. To learn more about the Resource Graph language, continue to the query language details page.