|
1 | 1 | ---
|
2 |
| -title: "Quickstart: Create a shared query with Azure CLI" |
3 |
| -description: In this quickstart, you follow the steps to enable the Resource Graph extension for Azure CLI and create a shared query. |
4 |
| -ms.date: 08/17/2021 |
| 2 | +title: "Quickstart: Create Resource Graph shared query using Azure CLI" |
| 3 | +description: In this quickstart, you create an Azure Resource Graph shared query using Azure CLI and the resource-graph extension. |
| 4 | +ms.date: 06/26/2024 |
5 | 5 | ms.topic: quickstart
|
6 | 6 | ms.custom: devx-track-azurecli
|
7 | 7 | ---
|
8 |
| -# Quickstart: Create a Resource Graph shared query using Azure CLI |
9 | 8 |
|
10 |
| -The first step to using Azure Resource Graph with [Azure CLI](/cli/azure/) is to check that the |
11 |
| -extension is installed. This quickstart walks you through the process of adding the extension to |
12 |
| -your Azure CLI installation. You can use the extension with Azure CLI installed locally or through |
13 |
| -the [Azure Cloud Shell](https://shell.azure.com). |
| 9 | +# Quickstart: Create Resource Graph shared query using Azure CLI |
14 | 10 |
|
15 |
| -At the end of this process, you'll have added the extension to your Azure CLI installation of choice |
16 |
| -and create a Resource Graph shared query. |
| 11 | +This quickstart describes how to create an Azure Resource Graph shared query with Azure CLI and the Resource Graph extension. The [az graph shared-query](/cli/azure/graph/shared-query) commands are an _experimental_ feature of [az graph query](/cli/azure/graph#az-graph-query). |
| 12 | + |
| 13 | +A shared query can be run from Azure CLI with the _experimental_ feature's commands, or you can run the shared query from the Azure portal. A shared query is an Azure Resource Manager object that you can grant permission to or run in Azure Resource Graph Explorer. When you finish, you can remove the Resource Graph extension. |
17 | 14 |
|
18 | 15 | ## Prerequisites
|
19 | 16 |
|
20 |
| -If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account |
21 |
| -before you begin. |
| 17 | +- If you don't have an Azure account, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 18 | +- [Azure CLI](/cli/azure/install-azure-cli) must be version 2.22.0 or higher for the Resource Graph extension. |
| 19 | +- A Bash shell environment where you can run Azure CLI commands. For example, Git Bash in a [Visual Studio Code](https://code.visualstudio.com/) terminal session. |
| 20 | + |
| 21 | +## Connect to Azure |
22 | 22 |
|
23 |
| -<!-- [!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)] --> |
| 23 | +From a Visual Studio Code terminal session, connect to Azure. If you have more than one subscription, run the commands to set context to your subscription. Replace `<subscriptionID>` with your Azure subscription ID. |
24 | 24 |
|
25 |
| -## Add the Resource Graph extension |
| 25 | +```azurecli |
| 26 | +az login |
| 27 | +
|
| 28 | +# Run these commands if you have multiple subscriptions |
| 29 | +az account list --output table |
| 30 | +az account set --subscription <subscriptionID> |
| 31 | +``` |
26 | 32 |
|
27 |
| -To enable Azure CLI to work with Azure Resource Graph, the extension must be added. This extension |
28 |
| -works wherever Azure CLI can be used, including [bash on Windows 10](/windows/wsl/install-win10), |
29 |
| -[Cloud Shell](https://shell.azure.com) (both standalone and inside the portal), the [Azure CLI |
30 |
| -Docker image](https://hub.docker.com/_/microsoft-azure-cli), or locally installed. |
| 33 | +## Install the extension |
31 | 34 |
|
32 |
| -1. Check that the latest Azure CLI is installed (at least **2.8.0**). If it isn't yet installed, |
33 |
| - follow [these instructions](/cli/azure/install-azure-cli-windows). |
| 35 | +To enable Azure CLI to query resources using Azure Resource Graph, the Resource Graph extension must be installed. The first time you run a query with `az graph` a prompt is displayed to install the extension. Otherwise, use the following steps to do a manual installation. |
34 | 36 |
|
35 |
| -1. In your Azure CLI environment of choice, use |
36 |
| - [az extension add](/cli/azure/extension#az-extension-add) to import the Resource Graph extension |
37 |
| - with the following command: |
| 37 | +1. List the available extensions and versions: |
38 | 38 |
|
39 | 39 | ```azurecli
|
40 |
| - # Add the Resource Graph extension to the Azure CLI environment |
41 |
| - az extension add --name resource-graph |
| 40 | + az extension list-available --output table |
42 | 41 | ```
|
43 | 42 |
|
44 |
| -1. Validate that the extension has been installed and is the expected version (at least **1.1.0**) |
45 |
| - with [az extension list](/cli/azure/extension#az-extension-list): |
| 43 | +1. Install the extension: |
46 | 44 |
|
47 | 45 | ```azurecli
|
48 |
| - # Check the extension list (note that you may have other extensions installed) |
49 |
| - az extension list |
50 |
| -
|
51 |
| - # Run help for graph query options |
52 |
| - az graph query -h |
| 46 | + az extension add --name resource-graph |
53 | 47 | ```
|
54 | 48 |
|
55 |
| -## Create a Resource Graph shared query |
| 49 | +1. Verify the extension was installed: |
56 | 50 |
|
57 |
| -With the Azure CLI extension added to your environment of choice, it's time to a Resource Graph |
58 |
| -shared query. The shared query is an Azure Resource Manager object that you can grant permission to |
59 |
| -or run in Azure Resource Graph Explorer. The query summarizes the count of all resources grouped by |
60 |
| -_location_. |
| 51 | + ```azurecli |
| 52 | + az extension list --output table |
| 53 | + ``` |
61 | 54 |
|
62 |
| -1. Create a resource group with [az group create](/cli/azure/group#az-group-create) to store the |
63 |
| - Azure Resource Graph shared query. This resource group is named `resource-graph-queries` and the |
64 |
| - location is `westus2`. |
| 55 | +1. Display the extension's syntax: |
65 | 56 |
|
66 | 57 | ```azurecli
|
67 |
| - # Login first with az login if not using Cloud Shell |
68 |
| -
|
69 |
| - # Create the resource group |
70 |
| - az group create --name 'resource-graph-queries' --location 'westus2' |
| 58 | + az graph query --help |
71 | 59 | ```
|
72 | 60 |
|
73 |
| -1. Create the Azure Resource Graph shared query using the `graph` extension and |
74 |
| - [az graph shared-query create](/cli/azure/graph/shared-query#az-graph-shared-query-create) |
75 |
| - command: |
| 61 | + For more information about Azure CLI extensions, go to [Use and manage extensions with the Azure CLI](/cli/azure/azure-cli-extensions-overview). |
| 62 | + |
| 63 | +## Create a shared query |
| 64 | + |
| 65 | +Create a resource group and a shared that summarizes the count of all resources grouped by location. |
| 66 | + |
| 67 | +1. Create a resource group to store the Azure Resource Graph shared query. |
76 | 68 |
|
77 | 69 | ```azurecli
|
78 |
| - # Create the Azure Resource Graph shared query |
79 |
| - az graph shared-query create --name 'Summarize resources by location' \ |
80 |
| - --description 'This shared query summarizes resources by location for a pinnable map graphic.' \ |
81 |
| - --graph-query 'Resources | summarize count() by location' \ |
82 |
| - --resource-group 'resource-graph-queries' |
| 70 | + az group create --name "demoSharedQuery" --location westus2 |
83 | 71 | ```
|
84 | 72 |
|
85 |
| -1. List the shared queries in the new resource group. The |
86 |
| - [az graph shared-query list](/cli/azure/graph/shared-query#az-graph-shared-query-list) |
87 |
| - command returns an array of values. |
| 73 | +1. Create the shared query. |
88 | 74 |
|
89 | 75 | ```azurecli
|
90 |
| - # List all the Azure Resource Graph shared queries in a resource group |
91 |
| - az graph shared-query list --resource-group 'resource-graph-queries' |
| 76 | + az graph shared-query create --name "Summarize resources by location" \ |
| 77 | + --description "This shared query summarizes resources by location for a pinnable map graphic." \ |
| 78 | + --graph-query "Resources | summarize count() by location" \ |
| 79 | + --resource-group demoSharedQuery |
92 | 80 | ```
|
93 | 81 |
|
94 |
| -1. To get just a single shared query result, use the |
95 |
| - [az graph shared-query show](/cli/azure/graph/shared-query#az-graph-shared-query-show) |
96 |
| - command. |
| 82 | +1. List all shared queries in the resource group. |
97 | 83 |
|
98 | 84 | ```azurecli
|
99 |
| - # Show a specific Azure Resource Graph shared query |
100 |
| - az graph shared-query show --resource-group 'resource-graph-queries' \ |
101 |
| - --name 'Summarize resources by location' |
| 85 | + az graph shared-query list --resource-group demoSharedQuery |
102 | 86 | ```
|
103 | 87 |
|
104 |
| -1. Run the shared query in Azure CLI with the `{{shared-query-uri}}` syntax in an |
105 |
| - [az graph query](/cli/azure/graph#az-graph-query) command. |
106 |
| - First, copy the `id` field from the result of the previous `show` command. Replace |
107 |
| - `shared-query-uri` text in the example with the value from the `id` field, but leave the |
108 |
| - surrounding `{{` and `}}` characters. |
| 88 | +1. Limit the results to a specific shared query. |
109 | 89 |
|
110 | 90 | ```azurecli
|
111 |
| - # Run a Azure Resource Graph shared query |
112 |
| - az graph query --graph-query "{{shared-query-uri}}" |
| 91 | + az graph shared-query show --resource-group "demoSharedQuery" \ |
| 92 | + --name "Summarize resources by location" |
113 | 93 | ```
|
114 | 94 |
|
115 |
| - > [!NOTE] |
116 |
| - > The `{{shared-query-uri}}` syntax is a **Preview** feature. |
| 95 | +## Run the shared query |
| 96 | + |
| 97 | +You can use the Azure CLI experimental feature syntax or the Azure portal to run the shared query. |
| 98 | + |
| 99 | +### Use experimental feature to run shared query |
| 100 | + |
| 101 | +Run the shared query in Azure CLI with the `{{shared-query-uri}}` syntax in an `az graph query` command. You get the resource ID of your shared query and store it in a variable. The variable is used when you run the shared query. |
117 | 102 |
|
118 |
| -Another way to find Resource Graph shared queries is through the Azure portal. In the portal, use |
119 |
| -the search bar to search for "Resource Graph queries". Select the shared query. On the **Overview** |
120 |
| -page, the **Query** tab displays the saved query. The **Edit** button opens it in |
121 |
| -[Resource Graph Explorer](./first-query-portal.md). |
| 103 | +```azurecli |
| 104 | +sharedqueryid=$(az graph shared-query show --resource-group "demoSharedQuery" \ |
| 105 | + --name "Summarize resources by location" \ |
| 106 | + --query id \ |
| 107 | + --output tsv) |
| 108 | +
|
| 109 | +az graph query --graph-query "{{$sharedqueryid}}" |
| 110 | +``` |
| 111 | + |
| 112 | +You can use the `subscriptions` parameter to limit the results. |
| 113 | + |
| 114 | +```azurecli |
| 115 | +az graph query --graph-query "{{$sharedqueryid}}" --subscriptions 11111111-1111-1111-1111-111111111111 |
| 116 | +``` |
| 117 | + |
| 118 | +### Run the shared query from portal |
| 119 | + |
| 120 | +You can verify the shared query works using Azure Resource Graph Explorer. To change the scope, use the **Scope** menu on the left side of the page. |
| 121 | + |
| 122 | +1. Sign in to [Azure portal](https://portal.azure.com). |
| 123 | +1. Enter _resource graph_ into the search field at the top of the page. |
| 124 | +1. Select **Resource Graph Explorer**. |
| 125 | +1. Select **Open query**. |
| 126 | +1. Change **Type** to _Shared queries_. |
| 127 | +1. Select the query _Count VMs by OS_. |
| 128 | +1. Select **Run query** and the view output in the **Results** tab. |
| 129 | + |
| 130 | +You can also run the query from your resource group. |
| 131 | + |
| 132 | +1. In Azure, go to the resource group, _demoSharedQuery_. |
| 133 | +1. From the **Overview** tab, select the query _Count VMs by OS_. |
| 134 | +1. Select the **Results** tab. |
122 | 135 |
|
123 | 136 | ## Clean up resources
|
124 | 137 |
|
125 |
| -If you wish to remove the Resource Graph shared query, resource group, and extension from your Azure |
126 |
| -CLI environment, you can do so by using the following commands: |
| 138 | +To remove the resource group and shared query: |
| 139 | + |
| 140 | +```azurecli |
| 141 | +az group delete --name demoSharedQuery |
| 142 | +``` |
127 | 143 |
|
128 |
| -- [az graph shared-query delete](/cli/azure/graph/shared-query#az-graph-shared-query-delete) |
129 |
| -- [az group delete](/cli/azure/group#az-group-delete) |
130 |
| -- [az extension remove](/cli/azure/extension#az-extension-remove) |
| 144 | +To remove the Resource Graph extension, run the following command: |
131 | 145 |
|
132 | 146 | ```azurecli
|
133 |
| -# Delete the Azure Resource Graph shared query |
134 |
| -az graph shared-query delete --resource-group 'resource-graph-queries' \ |
135 |
| - --name 'Summarize resources by location' |
| 147 | +az extension remove --name resource-graph |
| 148 | +``` |
136 | 149 |
|
137 |
| -# Remove the resource group |
138 |
| -# WARNING: This command deletes ALL resources you've added to this resource group without prompting for confirmation |
139 |
| -az group delete --resource-group 'resource-graph-queries' --yes |
| 150 | +To sign out of your Azure CLI session: |
140 | 151 |
|
141 |
| -# Remove the Azure Resource Graph extension from the Azure CLI environment |
142 |
| -az extension remove -n resource-graph |
| 152 | +```azurecli |
| 153 | +az logout |
143 | 154 | ```
|
144 | 155 |
|
145 | 156 | ## Next steps
|
146 | 157 |
|
147 |
| -In this quickstart, you've added the Resource Graph extension to your Azure CLI environment and |
| 158 | +In this quickstart, you added the Resource Graph extension to your Azure CLI environment and |
148 | 159 | created a shared query. To learn more about the Resource Graph language, continue to the query
|
149 | 160 | language details page.
|
150 | 161 |
|
151 | 162 | > [!div class="nextstepaction"]
|
152 |
| -> [Get more information about the query language](./concepts/query-language.md) |
| 163 | +> [Understanding the Azure Resource Graph query language](./concepts/query-language.md) |
0 commit comments