Ayub
Ayub

Reputation: 21

How to create map(object) template for terraform resource "google_logging_metric" to pass all the object values from .tfvars file for multiple objects

How to create map(object) template for terraform resource "google_logging_metric" to pass all the object values from .tfvars file for multiple objects? sharing sample resource for "google_logging_metric" using main.tf file, .tfvars file for passing object values and variables file.

//main.tf file

resource "google_logging_metric" "logging_metric" {
  name   = "my-(custom)/metric"
  filter = "resource.type=gcs_bucket AND severity>=ERROR"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
    labels {
?????????
    }
  }
  label_extractors = {
??????????
  }
}


//.tfvars file

custom_logging_metrics = {
  metric1 = {
    name   = "my-custom-metric-1"
    filter = "resource.type=\"gce_instance\" AND severity>=ERROR AND log_name=\"projects/crafty-acumen-422607-j2/logs/OSConfigAgent\"\n"
    labels = {
      message = {
        value_type  = "STRING"
        description = "Error Message"
      }
      timestamp = {
        value_type  = "STRING"
        description = "time of error"
      }
    }
    label_extractors = {
      message   = "EXTRACT(jsonPayload.message)"
      timestamp = "EXTRACT(jsonPayload.localTimestamp)"
    }
  }
}


//variables.tf file
variable "custom_logging_metrics" {
  description = "Map of custom logging metrics with labels and extractors"
  type = map(object({
    name        = string
    filter      = string
    labels      = map(object({
      value_type  = string
      description = string
    }))
    label_extractors = map(string)
  }))
}

Upvotes: 0

Views: 36

Answers (0)

Related Questions