Quickstart: Create a shared query by using an ARM template

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. This quickstart uses an Azure Resource Manager template (ARM template) to create a shared query.

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 will open in the Azure portal.

Button to deploy the Resource Manager template to Azure.

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

Review the template

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, see 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:

Deploy the template

Note

Azure Resource Graph service is free. For more information, see Overview of Azure Resource Graph.

  1. Select the following image to sign in to the Azure portal and open the template:

    Button to deploy the Resource Manager template to Azure.

  2. 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.
    Location Select a region. For example, Central US.
    Query name Leave the default value: Count VMs by OS.
    Query code Leave the default value: Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)
    Query description Leave the default value: This shared query counts all virtual machine resources and summarizes by the OS type.
    I agree to the terms and conditions stated above (Select)
  3. Select Purchase.

Some other resources:

Validate the deployment

To run the new shared query, follow these steps:

  1. From the portal search bar, search for Resource Graph queries and select it.

  2. 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:

  1. From the portal search bar, search for Resource Graph Explorer and select it.

  2. Select the Open a query button.

  3. 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. Once the Count VMs by OS shared query is visible, select its name.

  4. Once the query is loaded, select the Run query button. Results are displayed in the Results tab.

Clean up resources

To remove the shared query created, follow these steps:

  1. From the portal search bar, search for Resource Graph queries and select it.

  2. Set the check box next to the shared query named Count VMs by OS.

  3. Select the Delete button along the top of the page.

Next steps

In this quickstart, you created a Resource Graph shared query.

To learn more about shared queries, continue to the tutorial for: