はじめに
Terraformでsynthetics_monitorを作成したのでメモしてみました!
今回編集するファイル
variable.tf
condition-synthetics_monitor.tf
policy.tf
手順
variable.tf.で変数の定義
variable "synthetics_monitor" {
type = map(string)
default = {
# URL
# Key:synthetics_statuscode
# Value:https://aaaa.aaaa.jp
synthetics_statuscode = "https://*****.*****.jp"
}
}
condition-synthetics_monitor.tfにsynthetics_monitorを記載
# -----------------------------
# # Syntheticsの設定
# -----------------------------
resource "newrelic_synthetics_monitor" "synthetics_statuscode" { #"リソースの指定でnewrelicのモニターを指定、teraform内の設定で一意になる名前"
status = "ENABLED" # モニタのステータス("ENABLED" または "DISABLED")
name = "monitor" # monitorのタイトル
period = "EVERY_MINUTE" #モニタの実行頻度
uri = var.synthetics_monitor.synthetics_statuscode #monitorの対象となるURL
type = "SIMPLE" #Syntheticsモニタの種類を指定(SIMPLE / BROWSER / SCRIPT_BROWSER / SCRIPT_API)
locations_public = ["AP_NORTHEAST_1"] #モニタの実行地域
}
# -----------------------------
# # Conditionの設定
# -----------------------------
resource "newrelic_nrql_alert_condition" "synthetics_statuscode" {
policy_id = newrelic_alert_policy.alert_policy.id
name = "外形監視"
violation_time_limit_seconds = 3600
aggregation_window = 600
aggregation_timer = 60
aggregation_method = "event_timer"
slide_by = 60
nrql {
query = "SELECT count(*) FROM SyntheticCheck FACET monitorName, location, result WHERE result = 'FAILED' AND monitorName IN ('${var.synthetics_monitor.synthetics_statuscode}')"
}
critical {
operator = "above"
threshold = 9
threshold_duration = 600
threshold_occurrences = "all"
}
}
policy.tfにポリシーの設定
resource "newrelic_alert_policy" "alert_policy" {
name = var.project_name
incident_preference = "PER_CONDITION_AND_TARGET" #ポリシー内のconditionを個別に検知してくれて、異なるインシデントとして扱うことができる。
}
参考
https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/synthetics_monitor
https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/nrql_alert_condition