This is an example to show how Infrastructure as Code (IaC) can be used to create Azure Dashboards and Alerts.
The full example contains a azure pipeline definition so it can be executed as a pipeline in Azure DevOps. If used, a Service Connection in Azure DevOps needs to be created with the name MySubscription.
- Create Resource Group and Storage Account to save Terraform State file.
- Initialize Terraform with backend in Azure
- Set some environment variables
- Do Terraform Plan
- Do Terraform Apply
- Create an App Service and a SQL Database to have something to show in the Dashboard.
- Create the Dashboard (more info below)
- Create an Action Group that we be used to send Alerts to
- Create an Alert (more info below)
Terraform resource azurerm_dashboard is used to define a dashboard. Because the definition of a dashboard is done in json and can be rather large, a template file is used, dashboard-template.tpl, to store the dashboard definition.
Variables can be injected from the Terraform resource into the template file.
main.tf
resource "azurerm_dashboard" "dashboard" {
...
dashboard_properties = templatefile("dashboard-template.tpl",
{
appservice_name = azurerm_app_service.app_service.name
...
})
}
dashboard-template.tpl
"metrics": [
{
"resourceMetadata": {
"id": "${appservice_id}"
},
"name": "BytesReceived",
"aggregationType": 1,
"metricVisualization": {
"displayName": "Data In",
"resourceDisplayName": "${appservice_name}"
}
}
],
More information how the Dashboard Json in structured can be found here: Programmatically create Azure Dashboards.
Terraform resource azurerm_monitor_metric_alert is used to define an alert.
TIP: Create the alert in the Azure Portal and export the ARM Template. Look into that template to find the right values that should be set in the Terraform resource.
Then create a monitor action group azurerm_monitor_action_group where the definition how the alert, if triggered, should be broadcasted.