Skip to content

Commit 6e5d8ee

Browse files
committed
add suggestions
1 parent a1bdf43 commit 6e5d8ee

1 file changed

Lines changed: 167 additions & 43 deletions

File tree

src/content/docs/azure/services/resource-graph.mdx

Lines changed: 167 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@ import AzureFeatureCoverage from "../../../../components/feature-coverage/AzureF
1111
Azure Resource Graph is a service for querying Azure resources at scale using a structured query language.
1212
It helps you search, filter, and project resource metadata across subscriptions.
1313
Resource Graph is useful for inventory, governance checks, and automated analysis workflows.
14+
For more information, see [Azure Resource Graph overview](https://learn.microsoft.com/en-us/azure/governance/resource-graph/overview).
1415

15-
LocalStack for Azure allows you to build and test Resource Graph workflows in your local environment.
16-
The supported APIs are available on our [API Coverage section](#api-coverage), which provides information on the extent of Resource Graph's integration with LocalStack.
16+
LocalStack for Azure enables users to explore resources deployed within the local environment using [Kusto Query Language (KQL)](https://learn.microsoft.com/en-us/azure/governance/resource-graph/concepts/query-language#supported-tabulartop-level-operators).
1717

1818
## Getting started
1919

2020
This guide is designed for users new to Resource Graph and assumes basic knowledge of the Azure CLI and our `azlocal` wrapper script.
2121

22-
Start your LocalStack container using your preferred method.
23-
Then start CLI interception:
22+
Launch LocalStack using your preferred method. For more information, see [Introduction to LocalStack for Azure](/azure/getting-started/). Once the container is running, enable Azure CLI interception by running:
2423

2524
```bash
26-
azlocal start_interception
25+
azlocal start-interception
2726
```
2827

28+
This command points the `az` CLI away from the public Azure management REST API and toward the LocalStack for Azure emulator API.
29+
To revert this configuration, run:
30+
31+
```bash
32+
azlocal stop-interception
33+
```
34+
35+
This reconfigures the `az` CLI to send commands to the official Azure management REST API.
36+
2937
### Create a resource group
3038

3139
Create a resource group for the resources you want to query:
@@ -109,69 +117,185 @@ az webapp create \
109117

110118
### Query resources with Resource Graph
111119

112-
The following queries mirror the same patterns used in our validated tests:
120+
The Resource Graph extension must be installed to use `az graph` commands. Refer to the [official installation instructions](https://learn.microsoft.com/en-us/azure/governance/resource-graph/first-query-azurecli#install-the-extension). Use the [az graph query](https://learn.microsoft.com/en-us/cli/azure/graph?view=azure-cli-latest#az-graph-query) command to run Kusto Query language (KQL) queries against the emulator.
121+
The following examples demonstrate common query patterns:
113122

114-
- filter by type: `where type =~ 'Microsoft.Web/sites'`
115-
- filter by type and name: `where type =~ 'Microsoft.Web/sites' and name =~ 'ls-app-doc81'`
116-
- filter by name: `where name =~ 'ls-app-doc81'`
117-
- project only IDs: `where type =~ 'Microsoft.Web/sites' and name =~ 'ls-app-doc81' | project id`
123+
```bash
124+
az graph query \
125+
--graph-query "Resources | where type =~ 'Microsoft.Web/sites'"
126+
```
127+
128+
```bash title="Output"
129+
{
130+
"count": 1,
131+
"data": [
132+
{
133+
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-resourcegraph-demo/providers/Microsoft.Web/sites/ls-app-doc81",
134+
"location": "westeurope",
135+
"name": "ls-app-doc81",
136+
"properties": {},
137+
"resourceGroup": "rg-resourcegraph-demo",
138+
"subscriptionId": "00000000-0000-0000-0000-000000000000",
139+
"type": "Microsoft.Web/sites"
140+
}
141+
],
142+
"skip_token": null,
143+
"total_records": 1
144+
}
145+
```
118146

119-
For example, run a query for all web sites:
147+
Query a web site by type and name, projecting only the `id` column:
120148

121149
```bash
122-
az rest --method post \
123-
--url "http://management.localhost.localstack.cloud:4566/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01" \
124-
--headers "Content-Type=application/json" \
125-
--body "{\"subscriptions\":[\"00000000-0000-0000-0000-000000000000\"],\"query\":\"where type =~ 'Microsoft.Web/sites'\"}"
150+
az graph query \
151+
--graph-query "Resources | where type =~ 'Microsoft.Web/sites' and name =~ 'ls-app-doc81' | project id"
126152
```
127153

128154
```bash title="Output"
129155
{
130156
"count": 1,
131-
"data": {
132-
"columns": [
133-
{
134-
"name": "id",
135-
"type": "string"
136-
},
137-
{
138-
"name": "name",
139-
"type": "string"
140-
},
141-
...
142-
],
143-
"rows": [
144-
[
145-
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-resourcegraph-demo/providers/Microsoft.Web/sites/ls-app-doc81",
146-
"ls-app-doc81",
147-
"westeurope",
148-
...
149-
]
150-
]
151-
},
152-
...
153-
"totalRecords": 1
157+
"data": [
158+
{
159+
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-resourcegraph-demo/providers/Microsoft.Web/sites/ls-app-doc81",
160+
"resourceGroup": "rg-resourcegraph-demo"
161+
}
162+
],
163+
"skip_token": null,
164+
"total_records": 1
165+
}
166+
```
167+
168+
Count all resources in a resource group:
169+
170+
```bash
171+
az graph query \
172+
--graph-query "Resources | where resourceGroup =~ 'rg-resourcegraph-demo' | count"
173+
```
174+
175+
```bash title="Output"
176+
{
177+
"count": 1,
178+
"data": [
179+
{
180+
"Count": 2
181+
}
182+
],
183+
"skip_token": null,
184+
"total_records": 1
154185
}
155186
```
156187

157-
Or, run a query for an unknown resource name:
188+
List resources sorted by name with a row limit:
189+
190+
```bash
191+
az graph query \
192+
--graph-query "Resources | where resourceGroup =~ 'rg-resourcegraph-demo' | project name, type | order by name asc | limit 5"
193+
```
194+
195+
```bash title="Output"
196+
{
197+
"count": 2,
198+
"data": [
199+
{
200+
"name": "asp-doc81",
201+
"type": "Microsoft.Web/serverfarms"
202+
},
203+
{
204+
"name": "ls-app-doc81",
205+
"type": "Microsoft.Web/sites"
206+
}
207+
],
208+
"skip_token": null,
209+
"total_records": 2
210+
}
211+
```
212+
213+
Query a resource that does not exist:
214+
215+
```bash
216+
az graph query \
217+
--graph-query "Resources | where type =~ 'Microsoft.Web/sites' and name =~ 'doesnotexist'"
218+
```
219+
220+
```bash title="Output"
221+
{
222+
"count": 0,
223+
"data": [],
224+
"skip_token": null,
225+
"total_records": 0
226+
}
227+
```
228+
229+
### Query resources with the REST API
230+
231+
An alternative way to invoke Azure Resource Graph is to call its REST API directly using the [`az rest`](https://learn.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest#az-rest) command:
158232

159233
```bash
160234
az rest --method post \
161-
--url "http://management.localhost.localstack.cloud:4566/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01" \
235+
--url "http://azure.localhost.localstack.cloud:4566/providers/Microsoft.ResourceGraph/resources?api-version=2024-04-01" \
162236
--headers "Content-Type=application/json" \
163-
--body "{\"subscriptions\":[\"00000000-0000-0000-0000-000000000000\"],\"query\":\"where type =~ 'Microsoft.Web/sites' and name =~ 'doesnotexist'\"}"
237+
--body "{\"subscriptions\":[\"00000000-0000-0000-0000-000000000000\"],\"query\":\"Resources | where type=~'Microsoft.Web/sites'\", \"options\":{\"resultFormat\":\"objectArray\"}}"
164238
```
165239

166240
```bash title="Output"
167241
{
168-
"count": 0,
242+
"count": 1,
243+
"data": [
244+
{
245+
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-resourcegraph-demo/providers/Microsoft.Web/sites/ls-app-doc81",
246+
"location": "westeurope",
247+
"name": "ls-app-doc81",
248+
"properties": {},
249+
"resourceGroup": "rg-resourcegraph-demo",
250+
"subscriptionId": "00000000-0000-0000-0000-000000000000",
251+
"type": "Microsoft.Web/sites"
252+
}
253+
],
169254
"facets": [],
170255
"resultTruncated": "false",
171-
"totalRecords": 0
256+
"totalRecords": 1
172257
}
173258
```
174259

260+
## Features
261+
262+
The Resource Graph emulator supports the following features:
263+
264+
- **KQL query engine**: A built-in parser and executor for the Kusto Query Language (KQL) subset used by Azure Resource Graph.
265+
- **Tabular and object result formats**: The `resultFormat` option controls whether results are returned as a column/row table or as an array of objects.
266+
- **Scalar functions**: Built-in functions including `tolower`, `toupper`, `strlen`, `trim`, `substring`, `strcat`, `isnull`, `isnotnull`, `isempty`, `isnotempty`, `tostring`, `toint`, `tolong`, `todouble`, and `coalesce`.
267+
- **Comparison operators**: Full support for `==`, `!=`, `=~`, `!~`, `contains`, `!contains`, `contains_cs`, `!contains_cs`, `startswith`, `!startswith`, `endswith`, `!endswith`, `has`, `!has`, `in`, `!in`, and `matches regex`.
268+
- **Aggregate functions**: `count()`, `dcount()`, `countif()`, `sum()`, `sumif()`, `avg()`, `min()`, and `max()` for use in `summarize` stages.
269+
270+
## Limitations
271+
272+
- **Single table only**: The emulator queries the `Resources` table. Other Resource Graph tables (such as `ResourceContainers`, `AdvisorResources`, and `SecurityResources`) are not available.
273+
- **No data persistence across restarts**: Resource metadata is not persisted and is lost when the LocalStack emulator is stopped or restarted.
274+
275+
### Supported tabular operators
276+
277+
The table below lists the KQL tabular operators supported by Azure Resource Graph and their availability in the LocalStack emulator.
278+
For the full reference, see [Supported tabular/top-level operators](https://learn.microsoft.com/en-us/azure/governance/resource-graph/concepts/query-language#supported-tabulartop-level-operators).
279+
280+
| Operator | Supported | Notes |
281+
|---|---|---|
282+
| `count` | Yes | Returns a single row with the total number of input rows. |
283+
| `distinct` | Yes | Deduplicates rows by the specified columns. |
284+
| `extend` | Yes | Adds computed columns to the result set. |
285+
| `join` | No | Cross-table joins are not supported. The emulator does not implement `ResourceContainers` or other secondary tables. |
286+
| `limit` | Yes | Synonym of `take`. |
287+
| `mv-expand` | No | Array expansion into multiple rows is not supported. |
288+
| `order` | Yes | Synonym of `sort`. Supports `asc` and `desc` directions. |
289+
| `parse` | No | String parsing with pattern matching is not supported. |
290+
| `project` | Yes | Supports column selection and aliased expressions. |
291+
| `project-away` | Yes | Removes specified columns from the result set. |
292+
| `sort` | Yes | Synonym of `order`. |
293+
| `summarize` | Yes | Supports aggregate functions with an optional `by` clause. |
294+
| `take` | Yes | Synonym of `limit`. |
295+
| `top` | Yes | Returns the first N rows sorted by specified columns. |
296+
| `union` | No | Combining results from multiple tables is not supported. |
297+
| `where` | Yes | Filters rows using comparison, logical, and string operators. |
298+
175299
## API Coverage
176300

177301
<AzureFeatureCoverage service="Microsoft.ResourceGraph" client:load />

0 commit comments

Comments
 (0)