This document discusses how Azure Resource Graph can be used to efficiently manage Azure subscriptions at scale. It begins with an overview of management challenges at scale and introduces Azure Resource Graph as a solution. Key points covered include:
- Azure Resource Graph allows querying across subscriptions quickly and at scale to gain insights.
- The query syntax is based on Kusto and allows filtering, projecting, extending, and aggregating query results.
- Azure Resource Graph can be accessed via the Azure portal, PowerShell, Azure CLI and other methods.
- Graph queries can be converted to Azure policies using a tool called graph2policy to enforce governance at scale.
3. • Management at scale in Azure
• What we used to do
• Say hello to Azure Resource Graph
• Query syntax and basics
• ARG in the portal
• ARG outside the portal
• ARG and Azure Policy
• Q&A
Agenda
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
9. Typical script
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
Lookup for all resources of a specific type
• Get subscription list
• Change context for each subscription
• Query
$ErrorActionPreference = 'Stop'
$subcriptions = Get-AzSubscription
$results = $subcriptions | ForEach-Object {
$_ | Set-AzContext | Out-Null
Write-Host ('Scanning subscription {0}' -f $_.Name) -ForegroundColor Green
Get-AzResource -ResourceType 'Microsoft.Storage/storageAccounts'
}
#do something with $results
$results
18. Azure
Resource Graph
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
where operator
Filters to the subset of rows that satisfy a predicate.
https://docs.microsoft.com/en-
us/azure/kusto/query/whereoperator
// all web sites
Resources
| where type =~ "Microsoft.Web/sites"
// all resources not global or canada, excluding networkwatchers and
Microsoft insights types
Resources
| where location !contains 'global' and location !contains 'canada'
| where type !~ 'Microsoft.Network/networkwatchers'
| where type !startswith 'microsoft.insights/'
20. Azure
Resource Graph
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
extend operator
Create calculated columns and append them to the
result set.
https://docs.microsoft.com/en-
us/azure/kusto/query/extendoperator
// all web certificates that expires within 90 days
Resources
| where type =~ "Microsoft.Web/certificates" and
properties.expirationDate <= now(90d)
| extend expirationDate = tostring(properties.expirationDate)
| project subscriptionId, resourceGroup, name, location,
thumbprint = properties.thumbprint, expirationDate,
friendlyName = properties.friendlyName, subjectName =
properties.subjectName
| sort by expirationDate asc
21. Azure
Resource Graph
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
summarize operator
Produces a table that aggregates the content of the
input table.
https://docs.microsoft.com/en-
us/azure/kusto/query/summarizeoperator
// count of all resources by subscription and location
Resources
| summarize count() by subscriptionId, location
// count of storage accounts with HTTP enabled by location
Resources
| where type =~ 'Microsoft.Storage/storageAccounts'
| where properties.supportsHttpsTrafficOnly == 'false'
| summarize count = count() by location
22. Azure
Resource Graph
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
Querying over tags
Use tags.name or tags['name'] construct to query
tags on resources.
https://docs.microsoft.com/en-
us/azure/kusto/query/extendoperator
// return all resources with the value 'production' in the
'environment' tag
Resources
| where tags['environment'] =~ 'production'
| project subscriptionId, resourceGroup, name, tags
// return all resources where the tag 'environment' is not present
Resources
| where isempty(tags['environment'])
| project subscriptionId, resourceGroup, name, tags
27. PowerShell
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
How to use Azure Resource Graph in PowerShell
• Install Az modules
• Install Az.ResourceGraph module
• Use Search-AzGraph cmdlet
$pageSize = 100
$iteration = 0
$searchParams = @{
Query = 'where type =~ "Microsoft.Network/applicationGateways" | project id, subscriptionId, subscriptionDisplayName
, resourceGroup, name, sslCertificates = properties.sslCertificates | order by id'
First = $pageSize
Include = 'displayNames'
}
$results = do {
$iteration += 1
Write-Verbose "Iteration #$iteration"
$pageResults = Search-AzGraph @searchParams
$searchParams.Skip += $pageResults.Count
$pageResults
Write-Verbose $pageResults.Count
} while ($pageResults.Count -eq $pageSize)
28. Azure CLI
Theeasiest,mostefficientwaytomanageAzuresubscriptionsatscale
How to use Azure Resource Graph in Azure CLI
• Install Azure CLI
• Install resource-graph extension
• Use az graph query
// Request a subset of results, skipping 20 items and getting the next 10.
az graph query -q "where type =~ "Microsoft.Compute" | project name, tags" --first 10 --
skip 20
// Choose subscriptions to query.
az graph query -q "where type =~ "Microsoft.Compute" | project name, tags" –subscriptions
11111111-1111-1111-1111-111111111111, 22222222-2222-2222-2222-222222222222
Without the right tools, it can get messy pretty quickly
If you don’t have visibility and control over what is happening in your environments, things will get like this at some point
* Register the Newsletter for upcoming webinars around Azure Governance
MG:
• Highest assignation level
• Can have none, one or many subscriptions
• Enable assignation at MG level of:
Role Based Access Control (RBAC)
Azure Policy
Cost Management
Azure Security Center
And more
Policy:
Enable you to set strict rules over what resources and types people can create
Audit your environments for compliance
Take remediation if resources are non-compliant
Azure Resource Manager without the help of ARG currently supports queries over basic resource fields,
specifically - Resource name, ID, Type, Resource Group, Subscription, and Location.
Resource Manager also provides facilities for calling individual resource providers for detailed properties one resource at a time.
You can make it work, but it is not a fun job and requires you to go over each subscriptions one at a time and do your queries
It is not fun with 10 subscription, just imagine over 100s or 1000s of subscriptions
Azure Resource Graph is a service in Azure that is designed to extend Azure Resource Management by providing
efficient and performant resource exploration with the ability to query at scale across a given set of subscriptions
so that you can effectively govern your environment.
Azure Resource Graph powers Azure portal's search bar, the new browse 'All resources' experience, and Azure Policy's Change history visual diff. It's designed to help customers manage large-scale environments.
Ability to query resources with complex filtering, grouping, and sorting by resource properties.
Ability to iteratively explore resources based on governance requirements.
Ability to assess the impact of applying policies in a vast cloud environment.
Ability to detail changes made to resource properties (preview).
Access the properties returned by resource providers without needing to make individual calls to each resource provider.
View the last 14 days of change history made to the resource to see what properties changed and when. (preview)
It's important to understand that Azure Resource Graph's query language is based on the Kusto query language used by Azure Data Explorer.
Resource Graph supports all KQL data types, scalar functions, scalar operators, and aggregation functions. Specific tabular operators are supported by Resource Graph, some of which have different behaviors.
Azure Data Explorer capabilities is the backend of other services built on its powerful query language, including Azure Monitor logs, Application Insights, Time Series Insights, and Windows Defender Advanced Threat Protection.
NOTE: When limiting the join results with project, the property used by join to relate the two tables, subscriptionId in the above example, must be included in project.
Schema browser
visualization - charts
pin query visualization to dashboard
Get queries from github repository – vm without managed disks
Create dynamically query with restriction to : subscriptionId == '0eb8caba-9df3-4cdc-b951-a28f58890ab9'