Reputation: 21
getting error while creating nested map(object) template for monitoring_logging_metric in terraform GCP. Trying to create a template resource and passing the values of multiple objects through tfvars file.
main.tf file
resource "google_logging_metric" "logging_metric" {
for_each = var.custom_logging_metrics
name = each.value.name
filter = each.value.filter
metric_descriptor {
metric_kind = each.value.metric_kind
value_type = each.value.value_type
labels = each.value.labels
}
label_extractors = each.value.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"
metric_kind = "DELTA"
value_type = "INT64"
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
metric_kind = string
value_type = string
label_extractors = map(string)
labels = map(string)
}))
}
output: ╷ │ Error: Unsupported argument │ │ on main.tf line 441, in resource "google_logging_metric" "logging_metric": │ 441: labels = each.value.labels │ │ An argument named "labels" is not expected here. Did you mean to define a block of type "labels"?
Upvotes: 0
Views: 112
Reputation: 14759
The error message tells you exactly what you should do in this case. If you go to the documentation of the google_logging_metric
resource you will see that the labels field is a block and not a map. Furthermore, you can make the description field optional within the labels map optional, so that it will be set to null if not supplied. Below you'll find an example.
main.tf
resource "google_logging_metric" "logging_metric" {
for_each = var.custom_logging_metrics
name = each.value.name
filter = each.value.filter
label_extractors = each.value.label_extractors
metric_descriptor {
metric_kind = each.value.metric_kind
value_type = each.value.value_type
dynamic "labels" {
for_each = each.value.labels
content {
key = labels.key
value_type = labels.value_type
description = labels.description
}
}
}
}
variables.tf
variable "custom_logging_metrics" {
description = "Map of custom logging metrics with labels and extractors"
type = map(object({
name = string
filter = string
metric_kind = string
value_type = string
label_extractors = map(string)
labels = map(object({
key = string
value_type = string
description = optional(string)
}
}))
}
Upvotes: 0