|
| 1 | +--- |
| 2 | +title: Create and share a query in Azure portal |
| 3 | +description: In this tutorial, learn to create a Resource Graph Query and share it with others in the Azure portal. |
| 4 | +author: DCtheGeek |
| 5 | +ms.author: dacoulte |
| 6 | +ms.date: 10/23/2019 |
| 7 | +ms.topic: tutorial |
| 8 | +ms.service: resource-graph |
| 9 | +--- |
| 10 | +# Tutorial: Create and share an Azure Resource Graph query in Azure portal |
| 11 | + |
| 12 | +Azure Resource Graph Explorer lets you save your Resource Graph queries right in Azure portal. There |
| 13 | +are two types of queries, _Private_ and _Shared_. A _Private_ query is saved in your Azure portal |
| 14 | +settings, but a _Shared_ query is a Resource Manager resource that can be managed with role-based |
| 15 | +access controls (RBAC) and protected with resource locks. |
| 16 | + |
| 17 | +Saving queries in Azure portal saves your time spent looking for your favorite or commonly used |
| 18 | +queries. When sharing queries, you enable your team to be consistent and repeatable. In this |
| 19 | +tutorial, you'll complete these steps: |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> - Create and delete a _Private_ query |
| 23 | +> - Create a _Shared_ query |
| 24 | +> - Discover _Shared_ queries |
| 25 | +> - Delete a _Shared_ query |
| 26 | +
|
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +To complete this tutorial, you need an Azure subscription. If you don't have an Azure subscription, |
| 30 | +create a [free account](https://azure.microsoft.com/free/) before you begin. |
| 31 | + |
| 32 | +## Create and delete a Private query |
| 33 | + |
| 34 | +_Private_ queries are only accessible or visible to the account that creates them. As they're saved |
| 35 | +in an account's Azure portal settings, they can only be created, used, and deleted from inside Azure |
| 36 | +portal. A _Private_ query isn't a Resource Manager resource. Create a new _Private_ query by |
| 37 | +following these steps: |
| 38 | + |
| 39 | +1. From the portal menu, select 'All services' or use the Azure search box at the top of all pages. |
| 40 | + Search for and select 'Resource Graph Explorer'. |
| 41 | + |
| 42 | +1. In the 'Query 1' tab on the Azure Resource Graph Explorer page, enter the following query. For |
| 43 | + information about this query, see |
| 44 | + [Samples - Count virtual machines by OS type](../samples/starter.md#count-virtual-machines-by-os-type). |
| 45 | + Select **Run query** to see the query results in th lower pane. |
| 46 | + |
| 47 | + ```kusto |
| 48 | + Resources |
| 49 | + | where type =~ 'Microsoft.Compute/virtualMachines' |
| 50 | + | summarize count() by tostring(properties.storageProfile.osDisk.osType) |
| 51 | + ``` |
| 52 | + |
| 53 | +1. Select **Save** or **Save as**, enter the _Name_ as 'Count VMs by OS', leave _Type_ as 'Private |
| 54 | + query', then select **Save** at the bottom of the _Save query_ pane. The title of the tab changes |
| 55 | + from 'Query 1' to 'Count VMs by OS'. |
| 56 | + |
| 57 | +1. Browse away from Azure Resource Graph Explorer in Azure portal and then return to it. The saved |
| 58 | + query is no longer displayed and the 'Query 1' tab has returned. |
| 59 | + |
| 60 | +1. Select **Open a query**. Check that _Type_ is 'Private query'. The saved 'Count VMs by OS' now |
| 61 | + appears in the _Query Name_ list. Select the title link of the saved query and it's loaded into a |
| 62 | + new tab with that queries name. |
| 63 | + |
| 64 | + > [!NOTE] |
| 65 | + > When a saved query is open and the tab shows it's _Name_, the **Save** button updates it with |
| 66 | + > any changes made. To create a new saved query, use **Save as** and follow the steps as if it |
| 67 | + > was a brand new saved query. |
| 68 | +
|
| 69 | +1. To delete the saved query, select **Open a query** again, and check that _Type_ is 'Private |
| 70 | + query'. On the row of the saved 'Count VMs by OS' query, select the trash can icon. On the |
| 71 | + confirmation dialog, select **Yes** to complete the deletion of the query. Then close the _Open a |
| 72 | + query_ pane. |
| 73 | + |
| 74 | +## Create a Shared query |
| 75 | + |
| 76 | +Unlike a _Private_ query, a _Shared_ query is a Resource Manager resource. This fact means the query |
| 77 | +gets saved to a resource group, can be managed and controlled with RBAC, and even protected with |
| 78 | +resource locks. As a resource, anyone with appropriate permissions can see and use it. Create a new |
| 79 | +_Shared_ query by following these steps: |
| 80 | + |
| 81 | +1. From the portal menu, select 'All services' or use the Azure search box at the top of all pages. |
| 82 | + Search for and select 'Resource Graph Explorer'. |
| 83 | + |
| 84 | +1. In the 'Query 1' tab on the Azure Resource Graph Explorer page, enter the following query. For |
| 85 | + information about this query, see |
| 86 | + [Samples - Count virtual machines by OS type](../samples/starter.md#count-virtual-machines-by-os-type). |
| 87 | + Select **Run query** to see the query results in the lower pane. |
| 88 | + |
| 89 | + ```kusto |
| 90 | + Resources |
| 91 | + | where type =~ 'Microsoft.Compute/virtualMachines' |
| 92 | + | summarize count() by tostring(properties.storageProfile.osDisk.osType) |
| 93 | + ``` |
| 94 | + |
| 95 | +1. Select **Save** or **Save as**. |
| 96 | + |
| 97 | +  |
| 98 | + |
| 99 | +1. In the _Save query_ pane, enter the _Name_ as 'Count VMs by OS', change _Type_ to 'Shared query', |
| 100 | + set _Description_ to 'Count of virtual machines by OS type', and select the _Subscription_ where |
| 101 | + the query resource gets created. Leave the 'Publish to resource-graph-queries resource group' |
| 102 | + checkbox checked and the _Resource Group location_ set to '(US) West Central US'. Then select |
| 103 | + **Save** at the bottom of the _Save query_ pane. The title of the tab changes from 'Query 1' to |
| 104 | + 'Count VMs by OS'. The first time 'resource-graph-queries' resource group is used, the save takes |
| 105 | + longer as the resource group is created. |
| 106 | + |
| 107 | +  |
| 108 | + |
| 109 | + > [!NOTE] |
| 110 | + > If desired, remove the check to provide the name of an existing resource group to save the |
| 111 | + > shared query into. Using the default named resource group for queries makes _Shared_ queries |
| 112 | + > easier to discover. It also makes more apparent the purpose of that resource group. However, |
| 113 | + > selecting an existing resource group may be done for security reasons based on existing |
| 114 | + > permissions. |
| 115 | +
|
| 116 | +1. Browse away from Azure Resource Graph Explorer in Azure portal and then return to it. The saved |
| 117 | + query is no longer displayed and the 'Query 1' tab has returned. |
| 118 | + |
| 119 | +1. Select **Open a query**. Check that _Type_ is 'Shared query' and the combination of |
| 120 | + _Subscription_ and _Resource group_ match where you saved the query. The saved 'Count VMs by OS' |
| 121 | + now appears in the _Query Name_ list. Select the title link of the saved query and it's loaded |
| 122 | + into a new tab with that queries name. As a _Shared_ query, it displays an icon in the tab next |
| 123 | + to the title denoting it as shared. |
| 124 | + |
| 125 | +  |
| 126 | + |
| 127 | + > [!NOTE] |
| 128 | + > When a saved query is open and the tab shows it's _Name_, the **Save** button updates it with |
| 129 | + > any changes made. To create a new saved query, use **Save as** and follow the steps as if it |
| 130 | + > was a brand new saved query. |
| 131 | +
|
| 132 | +## Discover Shared queries |
| 133 | + |
| 134 | +As a _Shared_ query is a Resource Manager resource, there are several ways to find them: |
| 135 | + |
| 136 | +- From Resource Graph Explorer, select **Open a query** and set _Type_ to 'Shared query' |
| 137 | +- The Resource Graph queries portal page |
| 138 | +- The resource group it was saved in |
| 139 | +- With a query to Resource Graph |
| 140 | + |
| 141 | +### View Resource Graph queries |
| 142 | + |
| 143 | +In Azure portal, the Resource Graph queries page displays _Shared_ queries that the logged in |
| 144 | +account has access to. This page allows filtering by name, subscription, resource group, and other |
| 145 | +properties of the Resource Graph query. Resource Graph queries can also be tagged, exported, and |
| 146 | +deleted using this interface. |
| 147 | + |
| 148 | +Selecting one of the queries opens the Resource Graph query page. Like other Resource Manager |
| 149 | +resources, this page offers an interactive overview along with the Activity log, access control, and |
| 150 | +tags. A resource lock can also be applied directly from this page. |
| 151 | + |
| 152 | +Get to the Resource Graph queries page from the portal menu by selecting 'All services' or using the |
| 153 | +Azure search box at the top of all pages. Search for and select 'Resource Graph Explorer'. |
| 154 | + |
| 155 | +### List Resource groups resources |
| 156 | + |
| 157 | +The Resource Graph query is listed alongside other resources that are part of a resource group. |
| 158 | +Selecting the Resource Graph query opens the page for that query. The ellipsis or right-click |
| 159 | +options work the same as the Resource Graph query page. |
| 160 | + |
| 161 | +### Query Resource Graph |
| 162 | + |
| 163 | +As a Resource Manager resource, Resource Graph queries can be found with a query to Resource Graph. |
| 164 | +The following Resource Graph query limits by type `Microsoft.ResourceGraph/queries`, and then uses |
| 165 | +`project` to list only the name, time modified, and the query itself: |
| 166 | + |
| 167 | +```kusto |
| 168 | +Resources |
| 169 | +| where type == "microsoft.resourcegraph/queries" |
| 170 | +| project name, properties.timeModified, properties.query |
| 171 | +``` |
| 172 | + |
| 173 | +## Delete a Shared query |
| 174 | + |
| 175 | +If a _Shared_ query is no longer needed, delete it. Deleting a _Shared_ query removes the actual |
| 176 | +Resource Manager resource. Any dashboards the results chart was pinned to now display an error |
| 177 | +message. When that error message is displayed, use the **Remove from dashboard** button to clean up |
| 178 | +your dashboard. |
| 179 | + |
| 180 | +A _Shared_ query can be deleted from the following interfaces: |
| 181 | +- Resource Graph queries page |
| 182 | +- Resource Graph query page |
| 183 | +- Resource Graph Explorer's Open a query page |
| 184 | +- Resource groups page |
| 185 | + |
| 186 | +## Clean up resources |
| 187 | + |
| 188 | +When you're finished with this tutorial, delete the _Private_ and _Shared_ queries you created if |
| 189 | +you no longer want them. |
| 190 | + |
| 191 | +## Next steps |
| 192 | + |
| 193 | +- Run your first query with [Azure portal](../first-query-portal.md) |
| 194 | +- Get more information about the [query language](../concepts/query-language.md) |
| 195 | +- Learn to [explore resources](../concepts/explore-resources.md) |
| 196 | +- See samples of [Starter queries](../samples/starter.md) |
| 197 | +- See samples of [Advanced queries](../samples/advanced.md) |
| 198 | +- Provide feedback on [UserVoice](https://feedback.azure.com/forums/915958-azure-governance) |
0 commit comments