diff --git a/main.tf b/main.tf index 42a302d..e5b5490 100644 --- a/main.tf +++ b/main.tf @@ -2,40 +2,27 @@ locals { nr_entity_prefix = var.newrelic_resource_name_prefix != "" ? format("%s-", var.newrelic_resource_name_prefix) : "" nr_entity_suffix = var.newrelic_resource_name_suffix != "" ? format("-%s", var.newrelic_resource_name_suffix) : "" - non_critical_apm_resources = merge( - { for key, value in var.simple_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_non_critical_apm_resources, false) }, - { for key, value in var.browser_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_non_critical_apm_resources, false) }, - { for key, value in var.script_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_non_critical_apm_resources, false) }, - { for key, value in var.step_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_non_critical_apm_resources, false) }, - { for key, value in var.broken_links_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_non_critical_apm_resources, false) }, - { for key, value in var.cert_check_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_non_critical_apm_resources, false) } - ) - - critical_apm_resources = merge( - { for key, value in var.simple_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_critical_apm_resources, false) }, - { for key, value in var.browser_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_critical_apm_resources, false) }, - { for key, value in var.script_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_critical_apm_resources, false) }, - { for key, value in var.step_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_critical_apm_resources, false) }, - { for key, value in var.broken_links_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_critical_apm_resources, false) }, - { for key, value in var.cert_check_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if coalesce(value.create_critical_apm_resources, false) } - ) - - non_critical_browser_application_alert = { for key, value in var.browser_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if lookup(value, "create_non_critical_browser_alert", false) } - critical_browser_application_alert = { for key, value in var.browser_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if lookup(value, "create_critical_browser_alert", false) } + all_monitors = merge(var.simple_monitors, var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) + + all_monitor_resources = merge(newrelic_synthetics_monitor.simple, newrelic_synthetics_script_monitor.script, newrelic_synthetics_step_monitor.step, newrelic_synthetics_broken_links_monitor.broken_links, newrelic_synthetics_cert_check_monitor.cert_check) + + # constructs the name string for each monitor in a single space to avoid repetition. + prefix_suffix_map = {for key, _ in local.all_monitors: key => "${local.nr_entity_prefix}${k}${local.nr_entity_suffix}"} + } data "newrelic_entity" "this" { - for_each = merge(local.non_critical_apm_resources, local.critical_apm_resources) + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources || var.create_critical_apm_resources} - name = each.key + name = local.prefix_suffix_map[each.key] domain = "APM" type = "APPLICATION" } data "newrelic_entity" "browser_application" { - for_each = merge(local.non_critical_browser_application_alert, local.critical_browser_application_alert) + for_each = {for key, value in var.browser_monitors: key => value if var.create_non_critical_browser_alert || var.create_critical_browser_alert} - name = each.key + name = local.prefix_suffix_map[each.key] domain = "BROWSER" type = "APPLICATION" } @@ -56,10 +43,10 @@ data "pagerduty_escalation_policy" "ep" { ########################## # Ping Monitor -resource "newrelic_synthetics_monitor" "all" { - for_each = { for key, value in var.simple_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } +resource "newrelic_synthetics_monitor" "simple" { + for_each = var.simple_monitors - name = each.key + name = local.prefix_suffix_map[each.key] type = each.value["type"] period = each.value["period"] status = each.value["status"] @@ -79,9 +66,9 @@ resource "newrelic_synthetics_monitor" "all" { # Browser Monitor resource "newrelic_synthetics_monitor" "browser" { - for_each = { for key, value in var.browser_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = var.browser_monitors - name = "Browser-${each.key}" + name = "Browser-${local.prefix_suffix_map[each.key]}" type = each.value["type"] period = each.value["period"] status = each.value["status"] @@ -104,9 +91,9 @@ resource "newrelic_synthetics_monitor" "browser" { # Script Monitor resource "newrelic_synthetics_script_monitor" "script" { - for_each = { for key, value in var.script_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = var.script_monitors - name = each.key + name = local.prefix_suffix_map[each.key] type = each.value["type"] period = each.value["period"] status = each.value["status"] @@ -122,9 +109,9 @@ resource "newrelic_synthetics_script_monitor" "script" { # Step Monitors resource "newrelic_synthetics_step_monitor" "step" { - for_each = { for key, value in var.step_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = var.step_monitors - name = each.key + name = local.prefix_suffix_map[each.key] enable_screenshot_on_failure_and_script = each.value["enable_screenshot_on_failure_and_script"] locations_public = each.value["locations_public"] period = each.value["period"] @@ -145,9 +132,9 @@ resource "newrelic_synthetics_step_monitor" "step" { # Broken links monitors resource "newrelic_synthetics_broken_links_monitor" "broken_links" { - for_each = { for key, value in var.broken_links_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = var.broken_links_monitors - name = each.key + name = local.prefix_suffix_map[each.key] uri = each.value["uri"] locations_public = each.value["locations_public"] period = each.value["period"] @@ -158,9 +145,9 @@ resource "newrelic_synthetics_broken_links_monitor" "broken_links" { # Certificate check monitors resource "newrelic_synthetics_cert_check_monitor" "cert_check" { - for_each = { for key, value in var.broken_links_monitors : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = var.cert_check_monitors - name = each.key + name = local.prefix_suffix_map[each.key] domain = each.value["domain"] locations_public = each.value["locations_public"] certificate_expiration = each.value["certificate_expiration"] @@ -172,14 +159,14 @@ resource "newrelic_synthetics_cert_check_monitor" "cert_check" { ########## resource "newrelic_alert_policy" "synthetics" { - for_each = { for key, value in merge(var.simple_monitors, var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor || value.create_critical_monitor } - name = "${each.key}-synthetics" + name = "${local.prefix_suffix_map[each.key]}-synthetics" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_notification_destination" "synthetics" { - for_each = { for key, value in merge(var.simple_monitors, var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor || value.create_critical_monitor } name = pagerduty_service.synthetics_newrelic[each.key].name type = "PAGERDUTY_SERVICE_INTEGRATION" @@ -195,16 +182,16 @@ resource "newrelic_notification_destination" "synthetics" { } resource "newrelic_notification_channel" "synthetics" { - for_each = { for key, value in merge(var.simple_monitors, var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor || value.create_critical_monitor } - name = each.key + name = local.prefix_suffix_map[each.key] type = "PAGERDUTY_SERVICE_INTEGRATION" destination_id = newrelic_notification_destination.synthetics[each.key].id product = "IINT" property { key = "summary" - value = "Monitor endpoints ${each.key} failling in 1-3 locations" + value = "Monitor endpoints ${local.prefix_suffix_map[each.key]} failling in 1-3 locations" } property { key = "policy_id" @@ -217,9 +204,9 @@ resource "newrelic_notification_channel" "synthetics" { } resource "newrelic_workflow" "this" { - for_each = { for key, value in merge(var.simple_monitors, var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor || value.create_critical_monitor } - name = "Monitor-${each.key}-health" + name = "Monitor-${local.prefix_suffix_map[each.key]}-health" muting_rules_handling = "NOTIFY_ALL_ISSUES" issues_filter { name = "workflow-filter" @@ -246,20 +233,15 @@ resource "newrelic_workflow" "this" { # Critical health alert condition resource "newrelic_nrql_alert_condition" "critical_health_synthetics" { - for_each = { for key, value in merge(var.simple_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_critical_monitor } policy_id = newrelic_alert_policy.synthetics[each.key].id - name = "${each.key}-Critical-monitor-health" + name = "${local.prefix_suffix_map[each.key]}-Critical-monitor-health" description = "critical-alert" enabled = true nrql { - query = "SELECT filter(count(*), WHERE result = 'FAILED') AS 'Failures' FROM SyntheticCheck WHERE entityGuid IN ('${ - lookup(each.value, "type") == "SIMPLE" ? newrelic_synthetics_monitor.all[each.key].id : - lookup(each.value, "type") == "SCRIPT_API" || lookup(each.value, "type") == "SCRIPT_BROWSER" ? newrelic_synthetics_script_monitor.script[each.key].id : - lookup(each.value, "type") == "STEP" ? newrelic_synthetics_step_monitor.step[each.key].id : - lookup(each.value, "type") == "BROKEN_LINKS" ? newrelic_synthetics_broken_links_monitor.broken_links[each.key].id : - newrelic_synthetics_cert_check_monitor.cert_check[each.key].id}') FACET monitorName" + query = "SELECT filter(count(*), WHERE result = 'FAILED') AS 'Failures' FROM SyntheticCheck WHERE entityGuid IN ('${local.all_monitor_resources[each.key].id}') FACET monitorName" } critical { operator = each.value["critical_synthetics_operator"] @@ -275,20 +257,15 @@ resource "newrelic_nrql_alert_condition" "critical_health_synthetics" { # Critical duration alert condition resource "newrelic_nrql_alert_condition" "critical_duration_synthetics" { - for_each = { for key, value in merge(var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_critical_monitor } policy_id = newrelic_alert_policy.synthetics[each.key].id - name = "${each.key}-Critical-monitor-duration" + name = "${local.prefix_suffix_map[each.key]}-Critical-monitor-duration" description = "critical-alert" enabled = true nrql { - query = "SELECT percentile(duration, 50) / 1000 FROM SyntheticCheck WHERE entityGuid IN ('${ - lookup(each.value, "type") == "BROWSER" ? newrelic_synthetics_monitor.browser[each.key].id : - lookup(each.value, "type") == "SCRIPT_API" || lookup(each.value, "type") == "SCRIPT_BROWSER" ? newrelic_synthetics_script_monitor.script[each.key].id : - lookup(each.value, "type") == "STEP" ? newrelic_synthetics_step_monitor.step[each.key].id : - newrelic_synthetics_broken_links_monitor.broken_links[each.key].id}') FACET monitorName" - + query = "SELECT percentile(duration, 50) / 1000 FROM SyntheticCheck WHERE entityGuid IN ('${local.all_monitor_resources[each.key].id}') FACET monitorName" } critical { operator = each.value["critical_duration_synthetics_operator"] @@ -310,20 +287,15 @@ resource "newrelic_nrql_alert_condition" "critical_duration_synthetics" { # Non critical health alert condition resource "newrelic_nrql_alert_condition" "noncritical_health_synthetics" { - for_each = { for key, value in merge(var.simple_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor } policy_id = newrelic_alert_policy.synthetics[each.key].id - name = "${each.key}-Non-Critical-monitor-health" + name = "${local.prefix_suffix_map[each.key]}-Non-Critical-monitor-health" description = "non-critical-alert" enabled = true nrql { - query = "SELECT filter(count(*), WHERE result = 'FAILED') AS 'Failures' FROM SyntheticCheck WHERE entityGuid IN ('${ - lookup(each.value, "type") == "SIMPLE" ? newrelic_synthetics_monitor.all[each.key].id : - lookup(each.value, "type") == "SCRIPT_API" || lookup(each.value, "type") == "SCRIPT_BROWSER" ? newrelic_synthetics_script_monitor.script[each.key].id : - lookup(each.value, "type") == "STEP" ? newrelic_synthetics_step_monitor.step[each.key].id : - lookup(each.value, "type") == "BROKEN_LINKS" ? newrelic_synthetics_broken_links_monitor.broken_links[each.key].id : - newrelic_synthetics_cert_check_monitor.cert_check[each.key].id}') FACET monitorName" + query = "SELECT filter(count(*), WHERE result = 'FAILED') AS 'Failures' FROM SyntheticCheck WHERE entityGuid IN ('${local.all_monitor_resources[each.key].id}') FACET monitorName" } warning { @@ -340,19 +312,15 @@ resource "newrelic_nrql_alert_condition" "noncritical_health_synthetics" { # Non critical duration alert resource "newrelic_nrql_alert_condition" "non_critical_duration_synthetics" { - for_each = { for key, value in merge(var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor } policy_id = newrelic_alert_policy.synthetics[each.key].id - name = "${each.key}-Non-Critical-monitor-duration" + name = "${local.prefix_suffix_map[each.key]}-Non-Critical-monitor-duration" description = "non-critical-alert" enabled = true nrql { - query = "SELECT percentile(duration, 50) / 1000 FROM SyntheticCheck WHERE entityGuid IN ('${ - lookup(each.value, "type") == "BROWSER" ? newrelic_synthetics_monitor.browser[each.key].id : - lookup(each.value, "type") == "SCRIPT_API" || lookup(each.value, "type") == "SCRIPT_BROWSER" ? newrelic_synthetics_script_monitor.script[each.key].id : - lookup(each.value, "type") == "STEP" ? newrelic_synthetics_step_monitor.step[each.key].id : - newrelic_synthetics_broken_links_monitor.broken_links[each.key].id}') FACET monitorName" + query = "SELECT percentile(duration, 50) / 1000 FROM SyntheticCheck WHERE entityGuid IN ('${local.all_monitor_resources[each.key].id}') FACET monitorName" } warning { @@ -374,9 +342,9 @@ resource "newrelic_nrql_alert_condition" "non_critical_duration_synthetics" { ########################## resource "pagerduty_service" "synthetics_newrelic" { - for_each = { for key, value in merge(var.simple_monitors, var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor || value.create_critical_monitor } - name = "NewRelic-synthetics-${each.key}" + name = "NewRelic-synthetics-${local.prefix_suffix_map[each.key]}" auto_resolve_timeout = "null" acknowledgement_timeout = 600 escalation_policy = data.pagerduty_escalation_policy.ep.id @@ -389,7 +357,7 @@ resource "pagerduty_service" "synthetics_newrelic" { } resource "pagerduty_service_integration" "synthetics_newrelic" { - for_each = { for key, value in merge(var.simple_monitors, var.browser_monitors, var.script_monitors, var.step_monitors, var.broken_links_monitors, var.cert_check_monitors) : "${local.nr_entity_prefix}${key}${local.nr_entity_suffix}" => value if value.create_non_critical_monitor || value.create_critical_monitor } + for_each = { for key, value in local.all_monitors : key => value if value.create_non_critical_monitor || value.create_critical_monitor } name = data.pagerduty_vendor.vendor["New Relic"].name service = pagerduty_service.synthetics_newrelic[each.key].id @@ -403,21 +371,22 @@ resource "pagerduty_service_integration" "synthetics_newrelic" { ########################## resource "newrelic_alert_policy" "critical_apm_response_time" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} - name = "APM-${each.key}-response-time-Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-response-time-Critical" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_alert_policy" "critical_apm_error_rate" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} - name = "APM-${each.key}-error-rate-Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-error-rate-Critical" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_notification_destination" "critical_apm" { - count = length(local.critical_apm_resources) > 0 ? 1 : 0 + # we create only one + count = length({for key, value in local.all_monitors: key => value if var.create_critical_apm_resources}) > 0 ? 1 : 0 name = "${pagerduty_service.critical["NewRelic"].name}-APM" type = "PAGERDUTY_SERVICE_INTEGRATION" @@ -433,9 +402,9 @@ resource "newrelic_notification_destination" "critical_apm" { } resource "newrelic_notification_channel" "critical_apm_response_time" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} - name = "APM-${each.key}-response-time-Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-response-time-Critical" type = "PAGERDUTY_SERVICE_INTEGRATION" destination_id = newrelic_notification_destination.critical_apm[0].id product = "IINT" @@ -455,9 +424,9 @@ resource "newrelic_notification_channel" "critical_apm_response_time" { } resource "newrelic_notification_channel" "critical_apm_error_rate" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} - name = "APM-${each.key}-error-rate-Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-error-rate-Critical" type = "PAGERDUTY_SERVICE_INTEGRATION" destination_id = newrelic_notification_destination.critical_apm[0].id product = "IINT" @@ -477,7 +446,7 @@ resource "newrelic_notification_channel" "critical_apm_error_rate" { } resource "newrelic_nrql_alert_condition" "critical_response_time" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} policy_id = newrelic_alert_policy.critical_apm_response_time[each.key].id name = "${data.newrelic_entity.this[each.key].name}-Critical-response-time" @@ -496,7 +465,7 @@ resource "newrelic_nrql_alert_condition" "critical_response_time" { } resource "newrelic_nrql_alert_condition" "critical_error_rate" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} policy_id = newrelic_alert_policy.critical_apm_error_rate[each.key].id name = "${data.newrelic_entity.this[each.key].name}-Critical-error-rate" @@ -515,7 +484,7 @@ resource "newrelic_nrql_alert_condition" "critical_error_rate" { } resource "newrelic_workflow" "critical_apm_response_time" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} name = "APM-${data.newrelic_entity.this[each.key].name}-Critical-response-time" muting_rules_handling = "NOTIFY_ALL_ISSUES" @@ -537,7 +506,7 @@ resource "newrelic_workflow" "critical_apm_response_time" { } resource "newrelic_workflow" "critical_apm_error_rate" { - for_each = local.critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_critical_apm_resources} name = "APM-${data.newrelic_entity.this[each.key].name}-Critical-error-rate" muting_rules_handling = "NOTIFY_ALL_ISSUES" @@ -566,7 +535,7 @@ resource "newrelic_workflow" "critical_apm_error_rate" { resource "newrelic_notification_destination" "non_critical_apm" { - count = length(local.non_critical_apm_resources) > 0 ? 1 : 0 + count = length({for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources}) > 0 ? 1 : 0 name = "${pagerduty_service.non_critical["NewRelic"].name}-APM" type = "PAGERDUTY_SERVICE_INTEGRATION" @@ -582,23 +551,23 @@ resource "newrelic_notification_destination" "non_critical_apm" { } resource "newrelic_alert_policy" "non_critical_apm_response_time" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} - name = "APM-${each.key}-response-time-Non_Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-response-time-Non_Critical" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_alert_policy" "non_critical_apm_error_rate" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} - name = "APM-${each.key}-error-rate-Non_Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-error-rate-Non_Critical" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_notification_channel" "non_critical_apm_response_time" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} - name = "APM-${each.key}-response-time-Non_Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-response-time-Non_Critical" type = "PAGERDUTY_SERVICE_INTEGRATION" destination_id = newrelic_notification_destination.non_critical_apm[0].id product = "IINT" @@ -619,9 +588,9 @@ resource "newrelic_notification_channel" "non_critical_apm_response_time" { } resource "newrelic_notification_channel" "non_critical_apm_error_rate" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} - name = "APM-${each.key}-error-rate-Non_Critical" + name = "APM-${local.prefix_suffix_map[each.key]}-error-rate-Non_Critical" type = "PAGERDUTY_SERVICE_INTEGRATION" destination_id = newrelic_notification_destination.non_critical_apm[0].id product = "IINT" @@ -640,7 +609,7 @@ resource "newrelic_notification_channel" "non_critical_apm_error_rate" { } resource "newrelic_nrql_alert_condition" "non_critical_response_time" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} policy_id = newrelic_alert_policy.non_critical_apm_response_time[each.key].id name = "${data.newrelic_entity.this[each.key].name}-Non_Critical-response-time" @@ -659,7 +628,7 @@ resource "newrelic_nrql_alert_condition" "non_critical_response_time" { } resource "newrelic_nrql_alert_condition" "non_critical_error_rate" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} policy_id = newrelic_alert_policy.non_critical_apm_error_rate[each.key].id name = "${data.newrelic_entity.this[each.key].name}-Non_Critical-error-rate" @@ -678,7 +647,7 @@ resource "newrelic_nrql_alert_condition" "non_critical_error_rate" { } resource "newrelic_workflow" "non_critical_apm_response_time" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} name = "APM-${data.newrelic_entity.this[each.key].name}-Non_Critical-response-time" muting_rules_handling = "NOTIFY_ALL_ISSUES" @@ -700,7 +669,7 @@ resource "newrelic_workflow" "non_critical_apm_response_time" { } resource "newrelic_workflow" "non_critical_apm_error_rate" { - for_each = local.non_critical_apm_resources + for_each = {for key, value in local.all_monitors: key => value if var.create_non_critical_apm_resources} name = "APM-${data.newrelic_entity.this[each.key].name}-Non_Critical-error-rate" muting_rules_handling = "NOTIFY_ALL_ISSUES" @@ -728,7 +697,7 @@ resource "newrelic_workflow" "non_critical_apm_error_rate" { ########################## resource "newrelic_notification_destination" "non_critical_browser" { - count = length(local.non_critical_browser_application_alert) > 0 ? 1 : 0 + count = length({for key, value in var.browser_monitors: key => value if var.create_non_critical_browser_alert}) > 0 ? 1 : 0 name = "${pagerduty_service.non_critical["NewRelic"].name}-Browser" type = "PAGERDUTY_SERVICE_INTEGRATION" @@ -744,16 +713,16 @@ resource "newrelic_notification_destination" "non_critical_browser" { } resource "newrelic_alert_policy" "non_critical_browser_pageload" { - for_each = local.non_critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_non_critical_browser_alert} - name = "Browser-${each.key}-Non-Critical-Pageload" + name = "Browser-${local.prefix_suffix_map[each.key]}-Non-Critical-Pageload" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_notification_channel" "non_critical_browser_pageload" { - for_each = local.non_critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_non_critical_browser_alert} - name = "Browser-${each.key}-Non-Critical-Pageload" + name = "Browser-${local.prefix_suffix_map[each.key]}-Non-Critical-Pageload" type = "PAGERDUTY_SERVICE_INTEGRATION" destination_id = newrelic_notification_destination.non_critical_browser[0].id product = "IINT" @@ -772,7 +741,7 @@ resource "newrelic_notification_channel" "non_critical_browser_pageload" { } resource "newrelic_nrql_alert_condition" "non_critical_browser_pageload" { - for_each = local.non_critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_non_critical_browser_alert} policy_id = newrelic_alert_policy.non_critical_browser_pageload[each.key].id name = "${data.newrelic_entity.browser_application[each.key].name}-Non-Critical-Browser-Pageload" @@ -791,7 +760,7 @@ resource "newrelic_nrql_alert_condition" "non_critical_browser_pageload" { } resource "newrelic_workflow" "non_critical_browser_pageload" { - for_each = local.non_critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_non_critical_browser_alert} name = "${data.newrelic_entity.browser_application[each.key].name}-Non-Critical-Browser-Pageload" muting_rules_handling = "NOTIFY_ALL_ISSUES" @@ -819,7 +788,7 @@ resource "newrelic_workflow" "non_critical_browser_pageload" { ########################## resource "newrelic_notification_destination" "critical_browser" { - count = length(local.critical_browser_application_alert) > 0 ? 1 : 0 + count = length({for key, value in var.browser_monitors: key => value if var.create_critical_browser_alert}) > 0 ? 1 : 0 name = "${pagerduty_service.critical["NewRelic"].name}-Browser" type = "PAGERDUTY_SERVICE_INTEGRATION" @@ -835,16 +804,16 @@ resource "newrelic_notification_destination" "critical_browser" { } resource "newrelic_alert_policy" "critical_browser_pageload" { - for_each = local.critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_critical_browser_alert} - name = "Browser-${each.key}-Critical-Pageload" + name = "Browser-${local.prefix_suffix_map[each.key]}-Critical-Pageload" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_notification_channel" "critical_browser_pageload" { - for_each = local.critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_critical_browser_alert} - name = "Browser-${each.key}-Critical-Pageload" + name = "Browser-${local.prefix_suffix_map[each.key]}-Critical-Pageload" type = "PAGERDUTY_SERVICE_INTEGRATION" destination_id = newrelic_notification_destination.critical_browser[0].id product = "IINT" @@ -863,7 +832,7 @@ resource "newrelic_notification_channel" "critical_browser_pageload" { } resource "newrelic_nrql_alert_condition" "critical_browser_pageload" { - for_each = local.critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_critical_browser_alert} policy_id = newrelic_alert_policy.critical_browser_pageload[each.key].id name = "${data.newrelic_entity.browser_application[each.key].name}-Critical-Browser-Pageload" @@ -882,7 +851,7 @@ resource "newrelic_nrql_alert_condition" "critical_browser_pageload" { } resource "newrelic_workflow" "critical_browser_pageload" { - for_each = local.critical_browser_application_alert + for_each = {for key, value in var.browser_monitors: key => value if var.create_critical_browser_alert} name = "${data.newrelic_entity.browser_application[each.key].name}-Critical-Browser-Pageload" muting_rules_handling = "NOTIFY_ALL_ISSUES"