hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.6k stars 4.65k forks source link

Import azurerm_monitor_scheduled_query_rules_log fails with Wrong action type in Scheduled Query Rule ... insights.AlertingAction #14978

Open ekUSA opened 2 years ago

ekUSA commented 2 years ago

Community Note

Terraform (and AzureRM Provider) Version

Terraform v1.1.3 on windows_amd64

Affected Resource(s)

Terraform Configuration Files

resource "azurerm_monitor_scheduled_query_rules_log" "prod_alert" {
  name                   = "xyz errored"
  location               = var.location
  resource_group_name    = var.resource_monitor_group_name

  action {
    action_group           = [
        azurerm_monitor_action_group.prod_PagerDuty_ActGrp.id
        ]
    custom_webhook_payload = "{}"
  }

  data_source_id = azurerm_application_insights.envAppInsight.id
  description    = "Alert when total results cross threshold"
  enabled        = true
  # Count all requests with server error result code grouped into 5-minute bins
  query       = <<-QUERY
  exceptions
    | extend ApplicationName = tostring(customDimensions['ApplicationName'])
    | where ApplicationName == "xyz"
  QUERY
  severity    = 0
  frequency   = 5
  time_window = 5
  trigger {
    operator  = "GreaterThan"
    threshold = 0
  }

}

Debug Output

Panic Output

Expected Behaviour

Terraform should successfully import my existing App Insight Log Query Result Alert using this initial code for azurerm_monitor_scheduled_query_rules_log, so that I can figure out the correct option values and syntax for formatting to create other Azure Monitor Alerts

Actual Behaviour

Error: Wrong action type in Scheduled Query Rule "Daily M-F XYZ - Exception" (resource group "myRG"): insights.AlertingAction

Steps to Reproduce

terraform import azurerm_monitor_scheduled_query_rules_log.prod_alert "/subscriptions/xxxx/resourceGroups/myRG/providers/microsoft.insights/scheduledqueryrules/Daily M-F XYZ - Exception"

Important Factoids

in Azure Resource Graph Explorer, resources | where type == "microsoft.insights/scheduledqueryrules"

I see this for properties for the App Insight Log Query Monitor Alert I'm trying to import "odata.type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction",

References

https://github.com/hashicorp/terraform-provider-azurerm/issues/3951

But this example does not have action_type: https://github.com/hashicorp/terraform-provider-azurerm/issues/7174

The Terraform AzureRM docs do not mention how to use action group block nor alert type for azurerm_monitor_scheduled_query_rules_log and what are possible values https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_scheduled_query_rules_log should include example with some query syntax and multiple action groups for the alert

neil-yechenwei commented 2 years ago

@ekUSA , thanks for raising this issue.

I found seems your above terraform configuration is incorrect. There are many properties which are not in azurerm_monitor_scheduled_query_rules_log resource.

The example of Terraform document only expose the basic scenario not full scenario.

As the import example shows, you have to pass the name of the azurerm_monitor_scheduled_query_rules_log resource that is set in your terraform configuration file. So you cannot set it with query condition Daily M-F XYZ - Exception. See below example.

After tried to import existing resource azurerm_monitor_scheduled_query_rules_log, it succeeded with below terraform configuration and latest azurerm provider. Could you try below tf config to see if the issue still exists?

tf config:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "test" {
  name     = "acctestRG-monitor-test03"
  location = "eastus"
}

resource "azurerm_log_analytics_workspace" "test" {
  name                = "acctestWorkspace-test03"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  sku                 = "PerGB2018"
  retention_in_days   = 30
}

resource "azurerm_monitor_scheduled_query_rules_log" "test" {
  name                = "acctest msqrl1"
  resource_group_name = azurerm_resource_group.test.name
  location            = azurerm_resource_group.test.location

  data_source_id = azurerm_log_analytics_workspace.test.id

  criteria {
    metric_name = "Average_% Idle Time"
    dimension {
      name     = "InstanceName"
      operator = "Include"
      values   = ["1"]
    }
  }
}

tf import:

terraform import azurerm_monitor_scheduled_query_rules_log.test "/subscriptions/xx-xx-xx-xx/resourceGroups/acctestRG-monitor-test03/providers/Microsoft.Insights/scheduledQueryRules/acctest msqrl1"

Result: image