|
| 1 | +--- |
| 2 | +title: "Quickstart: Create a shared query with templates" |
| 3 | +description: In this quickstart, you use a Resource Manager template to create a Resource Graph shared query that counts virtual machines by OS. |
| 4 | +ms.date: 04/28/2020 |
| 5 | +ms.topic: quickstart |
| 6 | +ms.custom: subject-armqs |
| 7 | +--- |
| 8 | +# Quickstart: Create a shared query by using a Resource Manager template |
| 9 | + |
| 10 | +Resource Graph queries can be saved as a _private query_ or a _shared query_. A private query is |
| 11 | +saved to the individuals portal profile and isn't visible to others. A shared query is a Resource |
| 12 | +Manager object that can be shared with others through permissions and role-based access. A shared |
| 13 | +query provides common and consistent execution of resource discovery. This quickstart uses a |
| 14 | +Resource Manager template to create a shared query. |
| 15 | + |
| 16 | +[!INCLUDE [About Azure Resource Manager](../../../includes/resource-manager-quickstart-introduction.md)] |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account |
| 21 | +before you begin. |
| 22 | + |
| 23 | +## Create a shared query |
| 24 | + |
| 25 | +In this quickstart, you create a shared query called _Count VMs by OS_. To try this query in SDK or |
| 26 | +in portal with Resource Graph Explorer, see |
| 27 | +[Samples - Count virtual machines by OS type](./samples/starter.md#count-os). |
| 28 | + |
| 29 | +### Review the template |
| 30 | + |
| 31 | +The template used in this quickstart is from [Azure Quickstart templates](https://azure.microsoft.com/resources/templates/resourcegraph-sharedquery-countos/). |
| 32 | + |
| 33 | +:::code language="json" source="~/quickstart-templates/resourcegraph-sharedquery-countos/azuredeploy.json" highlight="28-37"::: |
| 34 | + |
| 35 | +The resource defined in the template is: |
| 36 | + |
| 37 | +- [Microsoft.ResourceGraph/queries](/azure/templates/microsoft.resourcegraph/queries) |
| 38 | + |
| 39 | +### Deploy the template |
| 40 | + |
| 41 | +> [!NOTE] |
| 42 | +> Azure Resource Graph service is free. For more information, see |
| 43 | +> [Overview of Azure Resource Graph](./overview.md). |
| 44 | +
|
| 45 | +1. Select the following image to sign in to the Azure portal and open the template: |
| 46 | + |
| 47 | + [](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fresourcegraph-sharedquery-countos%2Fazuredeploy.json) |
| 48 | + |
| 49 | +1. Select or enter the following values: |
| 50 | + |
| 51 | + | Name | Value | |
| 52 | + |------|-------| |
| 53 | + | Subscription | Select your Azure subscription. | |
| 54 | + | Resource group | Select **Create new**, specify a name, and then select **OK**. | |
| 55 | + | Location | Select a region. For example, **Central US**. | |
| 56 | + | Query name | Leave the default value **Count VMs by OS**. | |
| 57 | + | Query code | Leave the default value `Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)` | |
| 58 | + | Query description | Leave the default value **This shared query counts all virtual machine resources and summarizes by the OS type.** | |
| 59 | + | I agree to the terms and conditions stated above | (Select) | |
| 60 | + |
| 61 | +1. Select **Purchase**. |
| 62 | + |
| 63 | +Some additional resources: |
| 64 | + |
| 65 | +- To find more samples templates, see |
| 66 | + [Azure Quickstart template](https://azure.microsoft.com/resources/templates/?resourceType=Microsoft.Authorization&pageNumber=1&sort=Popular). |
| 67 | +- To see the template reference, go to |
| 68 | + [Azure template reference](/azure/templates/microsoft.resourcegraph/allversions). |
| 69 | +- To learn how to develop Resource Manager templates, see |
| 70 | + [Azure Resource Manager documentation](../../azure-resource-manager/management/overview.md). |
| 71 | +- To learn subscription-level deployment, see |
| 72 | + [Create resource groups and resources at the subscription level](../../azure-resource-manager/templates/deploy-to-subscription.md). |
| 73 | + |
| 74 | +## Validate the deployment |
| 75 | + |
| 76 | +To run the new shared query, follow these steps: |
| 77 | + |
| 78 | +1. From the portal search bar, search for **Resource Graph queries** and select it. |
| 79 | + |
| 80 | +1. Select the shared query named **Count VMs by OS**, then select the **Results** tab on the |
| 81 | + **Overview** page. |
| 82 | + |
| 83 | +Alternatively, the shared query can be opened from Resource Graph Explorer: |
| 84 | + |
| 85 | +1. From the portal search bar, search for **Resource Graph Explorer** and select it. |
| 86 | + |
| 87 | +1. Select the **Open a query** button. |
| 88 | + |
| 89 | +1. Change **Type** to _Shared queries_. If you don't see the **Count VMs by OS** in the list, use |
| 90 | + the filter box to limit the results. Once the **Count VMs by OS** shared query is visible, select |
| 91 | + its name. |
| 92 | + |
| 93 | +1. Once the query is loaded, select the **Run query** button. Results are displayed in the |
| 94 | + **Results** tab below. |
| 95 | + |
| 96 | +## Clean up resources |
| 97 | + |
| 98 | +To remove the shared query created, follow these steps: |
| 99 | + |
| 100 | +1. From the portal search bar, search for **Resource Graph queries** and select it. |
| 101 | + |
| 102 | +1. Set the check box next to the shared query named **Count VMs by OS**. |
| 103 | + |
| 104 | +1. Select the **Delete** button along the top of the page. |
| 105 | + |
| 106 | +## Next steps |
| 107 | + |
| 108 | +In this quickstart, you created a Resource Graph shared query. |
| 109 | + |
| 110 | +To learn more about shared queries, continue to the tutorial for: |
| 111 | + |
| 112 | +> [!div class="nextstepaction"] |
| 113 | +> [Manage queries in Azure portal](./tutorials/create-share-query.md) |
0 commit comments