Navigate back to the homepage

How to Create Azure Resource Graph Explorer Scheduled Reports and Email Alerts

John Kilmister
August 15th, 2021 · 2 min read

Azure has many ways to be notified of activity from budget alerts to Azure Monitor Alerts each of which can attach to action groups using the portal.

This is not the case when it comes to Azure Resource Graph Explorer. You can query resources that are deployed to Azure and produce reports and graphs to attach to dashboards. You cannot however set up scheduled reports, triggers and or action groups on these so easily.

Using Logic apps, a system assigned identity and the Azure Resource Graph Explorer Rest API we can quickly configure email reports and basic alerting.

Creating the Query

Inside the Azure Resource Graph Explorer you can create queries using the Kusto Query Language (KQL) that when run produce tables of data about your deployed resources. There are many uses from counting resources by their tags to finding unpatched machines.

Screenshot of Azure Website showing the Azure Resource Graph Explorer

In this demo we will use a simple query to count the number of key vaults in our subscription however the concepts are the same with more complex queries.

1Resources
2| where type =~ 'microsoft.keyvault/vaults'
3| count

Building the Logic App

The REST API can be consumed in many ways however by placing it in an Azure Logic app we can use a system managed identity making the authentication seamless.

Start by creating a consumption-based Logic app with a Recurrence trigger. For a scheduled report set the chosen interval, for an alert set the interval to the frequency you wish to check the condition.

Enable Managed Identity

Before we can continue, we must next enable Identity on the Logic App, which can be done from the left hand menu.

Screenshot of Azure Website showing the identity option

Once this is saved you will then see an Add Role Assignment option. For this query to run you will need to add a reader role at the subscription level.

HTTP API Request

Back in the logic app designer the next step is to add a HTTP Action block to make the API request. Use the following details and set the subscription ID of your subscription.

1Method: POST
2Uri: https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01
3Body:
4{
5 "query": "Resources | where type =~ 'microsoft.keyvault/vaults' | count",
6 "subscriptions": [
7 "00000000-0000-0000-0000-000000000000"
8 ]
9}

At the end of the HTTP Action block, you will see an option to Add new parameter. Select this and then check Authentication. Once you move off this you can choose the authentication type, which in this case needs to be Managed Identity.

Screenshot of Azure Website showing the HTTP block

A Parse JSON action block is next required to parse the results that come back from the API. For this block we can use the following schema.

1{
2 "properties": {
3 "count": {
4 "type": "integer"
5 },
6 "data": {
7 "properties": {
8 "columns": {
9 "items": {
10 "properties": {
11 "name": {
12 "type": "string"
13 },
14 "type": {
15 "type": "string"
16 }
17 },
18 "required": [
19 "name",
20 "type"
21 ],
22 "type": "object"
23 },
24 "type": "array"
25 },
26 "rows": {
27 "items": {
28 "items": {
29 "type": "integer"
30 },
31 "type": "array"
32 },
33 "type": "array"
34 }
35 },
36 "type": "object"
37 },
38 "facets": {
39 "type": "array"
40 },
41 "resultTruncated": {
42 "type": "string"
43 },
44 "totalRecords": {
45 "type": "integer"
46 }
47 },
48 "type": "object"
49}

Sending the Email

You could add a range of action blocks next to send a notification including connecting to MS teams or GMail. In this demo I have chosen to use an office 365 connection to send an email.

The data comes back as an array of rows with each item being an array of the column data. It is possible to use this to make a CSV report or build a HTML table.

In this example however we are only using a single value so we can get this with the expression body('Parse_JSON')?['data']?['rows'][0][0] which is taking the first row and first column.

Screenshot of Azure Website showing the email send block

Adding Conditions

So far this logic app will send an email every time it is triggered on the recurrence. To make this into an alert you can add in a condition to only send when a threshold is met. This is done using a Condition block and the previous expression to check the value skipping the email if the condition is not met.

Screenshot of Azure Website showing the conditionblock

Summary

Azure Resource Graph Explorer is very powerful however does not support scheduling email reports or alerting. Through the use of the REST API and a logic app we have seen how both email reports and alerting can be configured.

Title Photo by Stephen Phillips on Unsplash.

If you enjoyed reading please share...

More articles from John kilmister

Getting Started with Serverless SignalR and Azure Functions

SignalR provides the ability to push or broadcast information from a back-end server at any time to any front end web application that has…

July 28th, 2021 · 3 min read

Keeping up to date on Azure with MS Teams

With hundreds of services in Azure across many data centres it can be hard to keep up when items are introduced or updated. This includes…

July 9th, 2021 · 1 min read
Link to $https://twitter.com/johnkilmisterLink to $https://github.com/blueboxesLink to $https://stackoverflow.com/users/33/johnLink to $https://www.linkedin.com/in/johnkilmister/