Using Terraform I have created Alert, Action group and Logicapp. (Code-1 show below)
Action to trigger when an alert is triggered, where one of Action type is Logic app, which was as expected. Alert got triggered when condition is true further it trigger Logic app action.
Later, I got requirement to adjust alert’s threshold and frequency and made changes to Terraform code, while doing plan/apply noticed only changes in Alert Frequency and threshold value and these are expected. (Code-2 show below)
After this change(update) of Alert properties action as stopped functioning. when alert is triggered, Action is not triggered.
Test 1: Checking weather alert itself truly triggering
• Just to test the functionality of alert whether itself getting triggered, I subscribed Notification type to send mail to my Inbox. Which worked and I got mail regarding the alert but the alert unable to trigger action.
Test 1.2: Changes from Portal
• I removed testing-logicapp (Logic app action) under Action type from Azure Portal.
• And Added testing-logicapp (Logic app action) under Action type from Azure Portal. (As show in below JPG-3)
• This resulted Action to trigger when an alert is triggered.
So, my doubt is, how should I handle this scenario in terraform way.
Code-1 :
resource "azurerm_monitor_scheduled_query_rules_alert_v2" "myvm_task_alert_v2" {
name = "myvm-auto-deletion-alert"
resource_group_name = azurerm_resource_group.myvm_task_resource_group.name
evaluation_frequency = "PT1H"
window_duration = "PT1H"
scopes = [data.azurerm_log_analytics_workspace.azurerm_log_analytics_workspace.id]
severity = 4
criteria {
query = <<-QUERY
Perf
| where TimeGenerated > ago(1h)
| where CounterName == "% Processor Time" and InstanceName == "_Total"
| project TimeGenerated, Computer, CounterValue, _ResourceId
| summarize AggregatedValue = avg(CounterValue) by bin(TimeGenerated, 1h), Computer, _ResourceId
QUERY
time_aggregation_method = "Maximum"
threshold = 30.0
operator = "LessThan"
metric_measure_column = "AggregatedValue"
}
display_name = "myvm-auto-deletion-alert"
enabled = true
action {
action_groups = [azurerm_monitor_action_group.delete_myvm_action_group.id]
}
}
resource "azurerm_monitor_action_group" "delete_myvm_action_group" {
name = "myvm-auto-deletion-action-group"
resource_group_name = azurerm_resource_group.myvm_task_resource_group.name
short_name = "myvm-autodel"
tags = module.metadata.tags
logic_app_receiver {
name = "myvm-auto-deletion-logicapp"
resource_id = azurerm_logic_app_workflow.myvm_task_logicapp.id
callback_url = azurerm_logic_app_workflow.myvm_task_logicapp.access_endpoint
use_common_alert_schema = true
}
email_receiver {
name = "sendtoPraveen"
email_address = "[email protected]"
use_common_alert_schema = true
}
}
resource "azurerm_logic_app_workflow" "myvm_task_logicapp" {
name = "myvm-auto-deletion-logicapp"
location = "us east"
resource_group_name = azurerm_resource_group.myvm_task_resource_group.name
}
Code-2 :
resource "azurerm_monitor_scheduled_query_rules_alert_v2" "myvm_task_alert_v2" {
name = "myvm-auto-deletion-alert"
resource_group_name = azurerm_resource_group.myvm_task_resource_group.name
evaluation_frequency = "PT1H"
window_duration = "PT10H" ####################-------------------->change1
scopes = [data.azurerm_log_analytics_workspace.azurerm_log_analytics_workspace.id]
severity = 4
criteria {
query = <<-QUERY
Perf
| where TimeGenerated > ago(1h)
| where CounterName == "% Processor Time" and InstanceName == "_Total"
| project TimeGenerated, Computer, CounterValue, _ResourceId
| summarize AggregatedValue = avg(CounterValue) by bin(TimeGenerated, 1h), Computer, _ResourceId
QUERY
time_aggregation_method = "Maximum"
threshold = 50.0 ####################-------------------->change2
operator = "LessThan"
metric_measure_column = "AggregatedValue"
}
display_name = "myvm-auto-deletion-alert"
enabled = true
action {
action_groups = [azurerm_monitor_action_group.delete_myvm_action_group.id]
}
}
2
Answers
Found the issue with the help of MIcrosoft support team. Created new azurerm_logic_app_trigger_http_request flow.
You'll need to use callbackurl of azurerm_logic_app_trigger_http_request in Logic_app_receiver under actiongroup :
instead of
Ref : Action groups not triggering Logic Apps #6056
I tried it in my environment by using this terraform template for scheduled alerts and making a few modifications as required. It was successfully deployed, and an action was triggered with appropriate information to the specified user’s email address.
terraform init:
terraform plan:
terraform apply:
Deployed in Portal & added an action group:
Triggered alert mail successfully: