Skip to content

Creating a monitor that triggers on values greater than the threshold fails when the warning threshold is set lower in the TF #2285

Description

@gertjana

Datadog Terraform Provider Version

v1.6.1

Terraform Version

v3.35.0

What resources or data sources are affected?

datadog_monitor

Terraform Configuration Files

# providers.tf
terraform {
  required_version = "~> 1.5"
  backend "local" {}
  required_providers {
    datadog = {
      version = "3.35.0"
      source = "DataDog/datadog"
    }
  }
}
provider "datadog" {
  api_url = "https://api.datadoghq.eu"
  # keys left out
}

# variables.tf
variable "slo_applications_monitor" {
  type = list(object({
    name              = string
    query             = string
    threshold         = number
    threshold_warning = number
    timeframe         = string
  }))
  description = "Application Monitor based SLOs"
  default = [
    {
      name              = "my-app cpu"
      query             = "avg(last_7d):(avg:kubernetes.cpu.requests{kube_app_instance:my-app, env:prod*} / avg:kubernetes.cpu.limits{kube_app_instance:my-app, env:prod*}) > 90",
      threshold         = 90
      threshold_warning = 80
      timeframe         = "7d"
    },
  ]
}

# main.tf
resource "datadog_monitor" "slo_monitors" {
  for_each = { for idx, monitor in var.slo_applications_monitor : idx => monitor }

  name               = each.value.name
  type               = "query alert"
  message            = "Monitor triggered"
  escalation_message = "Escalation message"

  query = each.value.query

  monitor_thresholds {
    warning  = each.value.threshold_warning
    critical = each.value.threshold
  }
}

Relevant debug or panic output

Error: error creating service level objective from https://api.datadoghq.eu/api/v1/slo: 400 Bad Request: {"errors":["Invalid payload: warning must be greater than slo target value"]}

│ with datadog_service_level_objective.slo_applications_monitor["3"],
│ on main.tf line 40, in resource "datadog_service_level_objective" "slo_applications_monitor":
│ 40: resource "datadog_service_level_objective" "slo_applications_monitor" {

Expected Behavior

A successfull creation of the monitor

Actual Behavior

The error

Steps to Reproduce

I've created a monitor in the UI, then exported and copy pasted the query into the terraform variable

Important Factoids

Reproduced locally on a Macbook pro M2 sonoma and in gitlab CI/CD,
deploying to https://api.datadoghq.eu

References

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions