Azure Function App with PowerShell — Periodically Get Azure Service Health Events
Recently, I came across a need of pushing Azure service health events to other platforms, could be any 3rd party monitoring services. Since this is not supported natively, I thought about using Azure function app to periodically query the needed information. On top of that, users could write other codes/scripts to push the needed information to their services. At the end of day, users would need to have solution, so they could monitor all sorts of service events in one platform instead of going through Azure portal and Azure status site.
If you do not know what service health events are, please check out this site. There is another term called “resource health”. The main difference between these two is that service health focus on a broader scope, meaning when something is showing in the service health dashboard, it is not just happening to each individual’s resource alone.
There are mainly 4 kinds of event in service health.
- Service issues — Problems in the Azure services that affect you right now.
- Planned maintenance — Upcoming maintenance that can affect the availability of your services in the future.
- Health advisories — Changes in Azure services that require your attention. Examples include deprecation of Azure features or upgrade requirements (e.g upgrade to a supported PHP framework).
- Security advisories — Security related notifications or violations that may affect the availability of your Azure services.
The dashboard would be looking like below.
Back to the main topic, in order to push Azure service health events to other platform, we would need to leverage one of the following ways
- Az PowerShell/CLI
- REST API
- Other programming languages
to complete the process. In this article, we would focus on using Az PowerShell module and Azure Function App to complete the automation.
Prerequisite Actions
- Create an Azure Function App by following the steps mentioned here. Please ensure to use “PowerShell” as your runtime stack
- Configure the environment to have Az.ResourceGraph PowerShell module installed. The reason for installed Az.ResourceGraph module is that we are leveraging Azure Resource Graph API to query the needed information. If you would like to know more how this is done, please check this site. The same could also be accomplished via REST API.
Within the function app, search “advanced” and “Advanced Tools” would show.
Click on “Advanced Tools” and you would see “Go →”.
It would open a new tab in the browser and the URL would in the format of “<functionAppName>.scm.azurewebsites.net/Env/cshtml”. Go ahead to click on “Debug console” → “PowerShell”.
Within the PowerShell console, change the file path to “…/site/wwwroot”. There would be a pencil icon on the left of the file named “requirements.psd1”. Click on it.
- Remove the last line of ‘Az.*’ = ‘5.*’
- Replace it with 'Az.ResourceGraph' = '0.8.0' #The latest version#The content should look like below
# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
# For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'.
# To use the Az module in your function app, please uncomment the line below.
'Az.ResourceGraph' = '0.8.0'
}
After that, go all the way to the root directory and restart the whole function app.
Now, we are finally ready to go into the coding part.
Demonstration
Go down to the section “Functions” and click on “Functions”. Then, click on “+Add” and we should be seeing multiple templates. In this case, as I do not want to trigger via HTTP request, I would select Timer trigger.
Name whatever needed and set the timer frequency. The default would be firing every 5 minutes. If you would like to understand more what “0 */5 * * * *” means, please check out this site.
Within the function app, under “function.json”, we could again set the frequency we want.
Switch back to “run.ps1” and replace the content with below. Basically we would like to know whether the required module “Az.ResourceGraph” is installed and whether we could query recent Azure service health events.
# Input bindings are passed in via param block.
param($Timer)# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.if ($Timer.IsPastDue)
{
Write-Host "PowerShell timer is running late!"
}# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"#Write the installed module "Az.ResourceGraph"
Get-Module -Name Az.ResourceGraph | Write-Output#Write to host of the queries Azure service health events
Search-AzGraph -Query "servicehealthresources | where type == 'microsoft.resourcehealth/events' | project subscriptionId, Status=properties.Status, TrackingId=properties.TrackingId, EventType=properties.EventType, Title=properties.Title" | Write-Output
That is very simple way of leveraging both Az PowerShell module and Azure function app to achieve the goal of periodically getting the latest service health events. What is left is how to write more scripts/codes to push the output to other platforms but that is beyond today’s discussion. Happy learning!