|
1 | 1 | --- |
2 | 2 | title: "Resource Graph" |
3 | | -description: API coverage for Microsoft.ResourceGraph in LocalStack for Azure. |
| 3 | +description: Get started with Azure Resource Graph on LocalStack |
4 | 4 | template: doc |
5 | 5 | --- |
6 | 6 |
|
7 | 7 | import AzureFeatureCoverage from "../../../../components/feature-coverage/AzureFeatureCoverage"; |
8 | 8 |
|
| 9 | +## Introduction |
| 10 | + |
| 11 | +Azure Resource Graph is a service for querying Azure resources at scale using a structured query language. |
| 12 | +It helps you search, filter, and project resource metadata across subscriptions. |
| 13 | +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). |
| 15 | + |
| 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). |
| 17 | + |
| 18 | +## Getting started |
| 19 | + |
| 20 | +This guide is designed for users new to Resource Graph and assumes basic knowledge of the Azure CLI and our `azlocal` wrapper script. |
| 21 | + |
| 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: |
| 23 | + |
| 24 | +```bash |
| 25 | +azlocal start-interception |
| 26 | +``` |
| 27 | + |
| 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 | + |
| 37 | +### Create a resource group |
| 38 | + |
| 39 | +Create a resource group for the resources you want to query: |
| 40 | + |
| 41 | +```bash |
| 42 | +az group create \ |
| 43 | + --name rg-resourcegraph-demo \ |
| 44 | + --location westeurope |
| 45 | +``` |
| 46 | + |
| 47 | +```bash title="Output" |
| 48 | +{ |
| 49 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-resourcegraph-demo", |
| 50 | + "location": "westeurope", |
| 51 | + "managedBy": null, |
| 52 | + "name": "rg-resourcegraph-demo", |
| 53 | + "properties": { |
| 54 | + "provisioningState": "Succeeded" |
| 55 | + }, |
| 56 | + "tags": null, |
| 57 | + "type": "Microsoft.Resources/resourceGroups" |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Create sample web resources |
| 62 | + |
| 63 | +Create an App Service plan and a Web App, which we will query using Resource Graph: |
| 64 | + |
| 65 | +```bash |
| 66 | +az appservice plan create \ |
| 67 | + --name asp-doc81 \ |
| 68 | + --resource-group rg-resourcegraph-demo \ |
| 69 | + --location westeurope \ |
| 70 | + --sku F1 |
| 71 | + |
| 72 | +az webapp create \ |
| 73 | + --name ls-app-doc81 \ |
| 74 | + --resource-group rg-resourcegraph-demo \ |
| 75 | + --plan asp-doc81 \ |
| 76 | + --runtime "PYTHON:3.11" |
| 77 | +``` |
| 78 | + |
| 79 | +```bash title="Output" |
| 80 | +{ |
| 81 | + "asyncScalingEnabled": false, |
| 82 | + "elasticScaleEnabled": false, |
| 83 | + "geoRegion": "West Europe", |
| 84 | + "hyperV": false, |
| 85 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-resourcegraph-demo/providers/Microsoft.Web/serverfarms/asp-doc81", |
| 86 | + ... |
| 87 | + "name": "asp-doc81", |
| 88 | + ... |
| 89 | + "status": "Ready", |
| 90 | + "subscription": "00000000-0000-0000-0000-000000000000", |
| 91 | + ... |
| 92 | +} |
| 93 | +{ |
| 94 | + ... |
| 95 | + "enabledHostNames": [ |
| 96 | + "ls-app-doc81.azurewebsites.net", |
| 97 | + "ls-app-doc81.scm.azurewebsites.net" |
| 98 | + ], |
| 99 | + ... |
| 100 | + "hostNameSslStates": [ |
| 101 | + { |
| 102 | + "hostType": "Standard", |
| 103 | + "name": "ls-app-doc81.azurewebsites.net", |
| 104 | + "sslState": "Disabled", |
| 105 | + "thumbprint": null, |
| 106 | + "toUpdate": null, |
| 107 | + "virtualIp": null |
| 108 | + }, |
| 109 | + ... |
| 110 | + ], |
| 111 | + "hostNames": [ |
| 112 | + "ls-app-doc81.azurewebsites.net" |
| 113 | + ], |
| 114 | + ... |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Query resources with Resource Graph |
| 119 | + |
| 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: |
| 122 | + |
| 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 | +``` |
| 146 | + |
| 147 | +Query a web site by type and name, projecting only the `id` column: |
| 148 | + |
| 149 | +```bash |
| 150 | +az graph query \ |
| 151 | + --graph-query "Resources | where type =~ 'Microsoft.Web/sites' and name =~ 'ls-app-doc81' | project id" |
| 152 | +``` |
| 153 | + |
| 154 | +```bash title="Output" |
| 155 | +{ |
| 156 | + "count": 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 |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 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: |
| 232 | + |
| 233 | +```bash |
| 234 | +az rest --method post \ |
| 235 | + --url "http://azure.localhost.localstack.cloud:4566/providers/Microsoft.ResourceGraph/resources?api-version=2024-04-01" \ |
| 236 | + --headers "Content-Type=application/json" \ |
| 237 | + --body "{\"subscriptions\":[\"00000000-0000-0000-0000-000000000000\"],\"query\":\"Resources | where type=~'Microsoft.Web/sites'\", \"options\":{\"resultFormat\":\"objectArray\"}}" |
| 238 | +``` |
| 239 | + |
| 240 | +```bash title="Output" |
| 241 | +{ |
| 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 | + ], |
| 254 | + "facets": [], |
| 255 | + "resultTruncated": "false", |
| 256 | + "totalRecords": 1 |
| 257 | +} |
| 258 | +``` |
| 259 | + |
| 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 | + |
9 | 299 | ## API Coverage |
10 | 300 |
|
11 | 301 | <AzureFeatureCoverage service="Microsoft.ResourceGraph" client:load /> |
0 commit comments