Terraform Plan failing with variable validation

I am trying to create a GCP monitoring alert for composer disk usage. But in plan stage it is throwing me an error :

The given value is not valid for variable “conditions”: a number is required.

Variables.tf

Variable "conditions" {
  type = list(object({
    display_name         = string
    condition_threshold = list(object({
    display_name         = string
    comparison           = string
    duration             = number
    filter               = string
    threshold_value      = number
    alignment_period     = number
    cross_series_reducer = string
    group_by_fields      = list(string)
    per_series_aligner   = string
    trigger_count        = number
    trigger_percent      = number
  }))
}))
}

Tfvars file

conditions = [
   {display_name = "Condition for Composer"
    condition_threshold = [
        {
        display_name         = "Composer_cond"
        comparison           = "COMPARISON_GT"
        duration             = "0s"
        filter               = "resource.type = \"cloud_composer_environment\" AND resource.labels.project_id = \"prj-netw-01\" AND metric.type = \"composer.googleapis.com/environment/database/disk/bytes_used\""
      threshold_value      = 1600000000
      alignment_period     = "300s"
      cross_series_reducer = "REDUCE_NONE"
      group_by_fields =       ["project_id"]
      per_series_aligner   = "ALIGN_MEAN"
      trigger_count        = 1
      trigger_percent      = 100 
       }
   ]
 }
]

Alerting.tf

resource "google_monitoring_alert_policy" "alert_policy" {
  provider = google
  count = var.enable ? 1 : 0
  display_name = var.display_name
  combiner = var.combiner
 # threshold conditions
  dynamic "conditions" {
    for_each = var.conditions
    // iterator = condition
    content {
      display_name = condition.value.display_name

      dynamic "condition_threshold" {
        // for_each = { for con in each.value.conditions_threshold : con.display_name => con }
        for_each = lookup(conditions.value, "condition_threshold", [])
        content {
        comparison      = condition_threshold.value.comparison
        duration        = "${condition_threshold.value.duration}s"
        filter          = condition_threshold.value.filter
        threshold_value = condition_threshold.value.threshold_value

        aggregations {
          alignment_period     = "${condition_threshold.value.alignment_period}s"
          cross_series_reducer = condition_threshold.value.cross_series_reducer
          group_by_fields      = condition_threshold.value.group_by_fields
          per_series_aligner   = condition_threshold.value.per_series_aligner
        }

        trigger {
          count   = condition_threshold.value.trigger_count
          percent = condition_threshold.value.trigger_percent
        }
       }
      }
    }
  }

  notification_channels = var.notification_channels
}

Hi @singhsumeet361,

Your variable declaration specifies a number type for duration and alignment_period, but your are attempting to assign a string value to each of those. Assuming you need to use a unit for the timespans, you need to change the type to string. If the value is always in seconds, then you can change the assignment to the number of seconds.

1 Like

Thank you. It made the error go away. The plan stage is now passing without any errors.