I’ve manually created a contact point for Slack with template that works fine. Unfortunately it doesn’t render title or text body when I created the same in Terraform.
In my terraform I have:
resource "grafana_contact_point" "contact_point" {
name = "Terraform test"
slack {
title = "{{ template \"slack.title\" . }}"
text = grafana_message_template.slack_template.template
url = var.slack_webhook
username = "Grafana"
}
}
resource "grafana_message_template" "slack_template" {
name = "Terraform test"
template = <<EOT
{{ define "alert_severity_prefix_emoji" -}}
{{- if ne .Status "firing" -}}
:white_check_mark:
{{- else if eq .CommonLabels.severity "critical" -}}
:red_circle:
{{- else if eq .CommonLabels.severity "warning" -}}
:warning:
{{- end -}}
{{- end -}}
{{ define "slack.title" -}}
{{ template "alert_severity_prefix_emoji" . }}
[{{- .Status | toUpper -}}{{- if eq .Status "firing" }} x {{ .Alerts.Firing | len -}}{{- end }}]: {{ .CommonLabels.alertname -}}
{{- end -}}
{{- define "slack.text" -}}
{{- range .Alerts -}}
{{ if gt (len .Annotations ) 0 }}
*Description*: {{ .Annotations.description }}
*Instance*:
{{ range .Labels.SortedPairs }}{{ if or (eq .Name "env") (eq .Name "instance") }}• {{ .Name }}: `{{ .Value }}`
{{ end }}{{ end }}
*Silence alert*: {{ .SilenceURL }}
*Go to dashboard*: {{ .DashboardURL }}
*Go to panel directly*: {{ .PanelURL }}
{{ end }}
{{ end }}
{{ end }}
EOT
}
resource "grafana_notification_policy" "slack_notification_policy" {
group_by = ["..."]
contact_point = grafana_contact_point.contact_point.name
policy {
matcher {
label = "instance"
match = "="
value = "x"
}
contact_point = grafana_contact_point.contact_point.name
group_by = ["..."]
continue = false
}
policy {
matcher {
label = "instance"
match = "="
value = "y"
}
contact_point = "Slack" # <- Manually created
group_by = ["..."]
continue = false
}
}
This is how it looks in the UI:
And this is what I am getting in Slack: