Quickstart: Create a shared query using Bicep

Azure Resource Graph is an Azure service designed to extend Azure Resource Management by providing efficient and performant resource exploration with the ability to query at scale across a given set of subscriptions so you can effectively govern your environment. With Resource Graph queries, you can:

  • Query resources with complex filtering, grouping, and sorting by resource properties.
  • Explore resources iteratively based on governance requirements.
  • Assess the impact of applying policies in a vast cloud environment.
  • Query changes made to resource properties (preview).

Resource Graph queries can be saved as a private query or a shared query. A private query is saved to the individual's Azure 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 Bicep to create a shared query.

Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources. It provides concise syntax, reliable type safety, and support for code reuse. Bicep offers the best authoring experience for your infrastructure-as-code solutions in Azure.


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

Review the Bicep file

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 Bicep file used in this quickstart is from Azure Quickstart Templates.

@description('The name of the shared query.')
param queryName string = 'Count VMs by OS'

@description('The Azure Resource Graph query to be saved to the shared query.')
param queryCode string = 'Resources | where type =~ \'Microsoft.Compute/virtualMachines\' | summarize count() by tostring(properties.storageProfile.osDisk.osType)'

@description('The description of the saved Azure Resource Graph query.')
param queryDescription string = 'This shared query counts all virtual machine resources and summarizes by the OS type.'

resource query 'Microsoft.ResourceGraph/queries@2018-09-01-preview' = {
  name: queryName
  location: 'global'
  properties: {
    query: queryCode
    description: queryDescription

The resource defined in the Bicep file is:

Deploy the Bicep file

  1. Save the Bicep file as main.bicep to your local computer.


    The Bicep file isn't required to be named main.bicep. If you save the file with a different name, you must change the name of the template file in the deployment step below.

  2. Deploy the Bicep file using either Azure CLI or Azure PowerShell.

    az group create --name exampleRG --location eastus
    az deployment group create --resource-group exampleRG --template-file main.bicep

    When the deployment finishes, you should see a message indicating the deployment succeeded.

Some other resources:

Review deployed resources

Use the Azure portal, Azure CLI, or Azure PowerShell to list the deployed resources in the resource group.

az resource list --resource-group exampleRG

Clean up resources

When you no longer need the resource that you created, delete the resource group using Azure CLI or Azure PowerShell.

az group delete --name exampleRG

Next steps

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

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