I'm trying to write a log-based alert policy in terraform.
I want to generate an alert, in near real time, whenever a certain message appears in the logs. Specifically, I want to know when a Composer DAG fails.
I managed to successfully set up a log-based alert in the console with the following query filter:
resource.type="cloud_composer_environment"
severity="ERROR"
log_name="projects/my_project/logs/airflow-scheduler"
resource.labels.project_id="project-id"
textPayload=~"my_dag_name"
But, I am having trouble translating this log-based alert policy into terraform as a "google_monitoring_alert_policy".
I have tried to add the following filter conditions to the terraform google_monitoring_alert_policy
:
filter = "resource.type=cloud_composer_environment AND resource.label.project_id=${var.project} AND log_name=projects/${var.project}/logs/airflow-scheduler AND severity=ERROR AND textPayload=~my_dag_name"
But when running terraform apply
, I get the following error:
build 10-Nov-2022 12:21:00 [31mâ[0m [0m[1m[31mError: [0m[0m[1mError creating AlertPolicy: googleapi: Error 400: Field alert_policy.conditions[0].condition_threshold.filter had an invalid value of "resource.type=cloud_composer_environment AND resource.labels.project_id=my_project AND log_name=projects/my_project/logs/airflow-scheduler AND severity=ERROR AND textPayload=my_dag_name": The lefthand side of each expression must be prefixed with one of {group, metadata, metric, project, resource}.[0m
So I have two questions:
Can "log-based" alerts be configured in terraform at all?
How do I set up an alert in terraform that filters for a particular string in the log 'textPayload' field?