Hi,
I am trying to create a monitoring alert policy using json templates. The templates are in json format and i want to use them in the main monitoring resource. I am using jsondecode function to decode the files and read the content. But i cannot call them in the main resource. Below code fyr. Please let me know if you can suggest a correction.
A typical json files like below .
airflow-alert-policies.json
{
"displayName": "Airflow-Alert",
"combiner": "OR",
"conditions": [
{
"displayName": "Composer-Disk",
"conditionThreshold": {
"aggregations": [
{
"alignmentPeriod": "300s",
"crossSeriesReducer": "REDUCE_NONE",
"groupByFields": [
"project_id"
],
"perSeriesAligner": "ALIGN_MEAN"
}
],
"comparison": "COMPARISON_GT",
"duration": "0s",
"filter": "resource.type = \"cloud_composer_environment\" AND resource.labels.project_id = \"prj-airflow-01\" AND metric.type = \"composer.googleapis.com/environment/database/disk/bytes_used\"",
"thresholdValue": 1600000000,
"trigger": {
"count": 1
}
}
}
]
}
Alert.tf
locals {
alert_jsons = fileset(path.module, "alt/*.json")
alert_data = [for f in local.alert_jsons : jsondecode(file("${path.module}/${f}")) ]
condition_threshold = flatten ([for c in local.alert_data.conditions:
[for key,value in c.condition_threshold:
{
comparison = value.comparison
duration = value.duration
filter = value.filter
threshold_value = value.threshold_value
alignment_period = value.alignment_period
cross_series_reducer = value.cross_series_reducer
group_by_fields = value.group_by_fields
per_series_aligner = value.per_series_aligner
trigger_count = value.trigger_count
}]
])
}
resource "google_monitoring_alert_policy" "usecase_alert_policy" {
for_each = { for policy in local.alert_data: policy.display_name => display_name }
project = local.dataeng_project
combiner = each.value.combiner
dynamic "conditions" {
for_each = each.value.conditions
content {
dynamic "condition_threshold" {
content {
for_each = { for cndtn in local.condition_threshold : cndtn.display_name => cndtn }
}
}
}
}
}
Has anyone worked on a similar approach ? Can i use lookup to call the values inside the resource from json files.