Azure / terraform-provider-azapi

Terraform provider for Azure Resource Manager Rest API
https://registry.terraform.io/providers/Azure/azapi/latest
Mozilla Public License 2.0
170 stars 47 forks source link

resource "azapi_resource" returns "Error: Reference to undeclared resource" when creating metric Alert #378

Closed skwokie closed 4 months ago

skwokie commented 8 months ago

I have written the code below following https://learn.microsoft.com/en-us/azure/templates/microsoft.insights/metricalerts?pivots=deployment-language-terraform

variable "Env" {
  type    = string
  default = "Dev"
}

variable "Location" {
  type    = string
  default = "eastus"
}

variable "Subscription_id" {
  type    = string
}

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.2"
    }
    azapi = {
      source  = "azure/azapi"
      version = "~> 1.10.0"
    }
  }

  required_version = ">= 1.1.0"
}

provider "azurerm" {
  features {}
}

provider "azapi" {
}

resource "azurerm_resource_group" "Metric-Alerts" {
  name     = "Metric-Alerts-${var.Env}"
  location = var.Location
  tags = {
    Env = var.Env
  }
}

# the monitor alert
resource "azapi_resource" "PipelineFailedRuns_Alert" {
  type      = "Microsoft.Insights/metricAlerts@2018-03-01"
  name      = "PipelineFailedRuns-Failed-${var.Env}"
  location  = var.Location
  parent_id = azurerm_resource_group.Metric-Alerts.id

  body = jsonencode({
    properties = {
      severity = 1
      enabled  = true
      scopes   = [
        "/subscriptions/${var.subscription_id}/resourceGroups/DownloadDeviceModelFirmware-${var.Env}/providers/Microsoft.DataFactory/factories/ABCD-${var.Env}"
      ]
      criteria = {
        odata.type = "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria"
        allOf = [
          {
            threshold       = 0
            name            = "Metric1"
            metricNamespace = "Microsoft.DataFactory/factories"
            metricName      = "PipelineFailedRuns"
            dimensions = [
              {
                name     = "FailureType"
                operator = "Include"
                values = [
                  "*"
                ]
              },
              {
                name     = "Name"
                operator = "Include"
                values = [
                  "*"
                ]
              }
            ]
            operator             = "GreaterThan",
            timeAggregation      = "Total",
            skipMetricValidation = false,
            criterionType        = "StaticThresholdCriterion"
          }
        ]
      }
      evaluationFrequency = "PT5M"
      windowSize          = "PT15M"

      actions = [
        {
          actionGroupId = azurerm_monitor_action_group.send_metrics_alert.id
        }
      ]
    }
  })

  tags = {
    Env = var.Env
  }
}

However, I got the following error when running terraform apply:

│ Error: Reference to undeclared resource
│
│   on main.tf line 62, in resource "azapi_resource" "PipelineFailedRuns_Alert":
│   62:         odata.type = "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria"
│
│ A managed resource "odata" "type" has not been declared in the root module.

Thanks!

Terraform version: v1.5.7 OS: Alpine Linux (a runtime docker container) OS version: 3.16.2

ms-henglu commented 8 months ago

Hi @skwokie ,

Thank you for taking time to report this issue and apologize for late response!

It's not allowed to use the dot "." in a key directly, you could use the following format:

"odata.type" = "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria"

Then you could declare the this field in the config.

ms-henglu commented 4 months ago

Close this issue as it's resolved, but feel free to reopen it if there's any question.