Dynamic newrelic resource creation

hi guys,

I am dynamically creating newrelic_alert_condition by passing input parameters from my terragrunt wrapper on my base module. As part of this resource I want to create any variant of newrelic_alert_condition. However, for gc_cpu_time newrelic_alert_condition I need some extra/different attribute like gc_metric = "GC/G1 Young Generation" & violation_close_timer which isn’t required in other case. Is there a way I can do it within the same resource block or you guys recommend this to be a separate resource??


Add newrelic alert condition


locals {
l_newrelic_alert_condition = {
for obj in var.newrelic_alert_condition : “${obj.name} ${obj.policy_id} ${obj.type}” => merge(var.newrelic_alert_condition_defaults, obj)

resource “newrelic_alert_condition” “nr_alert” {

for_each = local.l_newrelic_alert_condition

policy_id = each.value.policy_id
type = each.value.type
name = each.value.name
entities = length(each.value.entities) == 0 ? [data.newrelic_application.app.id] : each.value.entities
metric = each.value.metric
gc_metric = try(each.value.gc_metric, null)
violation_close_timer = try(each.value.violation_close_timer, null)
condition_scope = each.value.condition_scope
term {
duration = each.value.term.duration
threshold = each.value.term.threshold
time_function = each.value.term.time_function

resource “newrelic_alert_condition” “gc_cpu_time” {
policy_id = length(var.gc_cpu_time_policy_id) > 0 ? var.gc_cpu_time_policy_id : newrelic_alert_policy.policy_name.id

name = “Excessive GC time”
type = “apm_jvm_metric”
metric = “gc_cpu_time”
entities = [data.newrelic_application.app.id]
gc_metric = “GC/G1 Young Generation”
runbook_url = “tbd”
condition_scope = “application”
violation_close_timer = 1

term {
duration = 5
operator = “above”
priority “critical”
threshold = “50”
time_function = “all”
count = var.jvm



Hi @deshdeep, generally speaking we recommend being explicit if it seems tricky to implement within Terraform’s constraints. In this case, I think you might need to use a separate resource. Even though Terraform supports conditionals for attribute values, I’m fairly certain Terraform does not support conditionally adding adding/removing attributes. There are some interesting ways to compose dynamic configurations, but I think you might have run into a limitation with Terraform. The only way I think this could be achieved is with Terraform’s nullable arguments feature, but it looks like you’ve already tried that already.

That being said, a quick Google search led me to this StackOverflow post that might help with different ideas of how to compose your configuration.

If by any chance you get something nifty built out and working, please share it here in case others come looking for something similar.

On a slightly different note, you could also attempt to do this with the newrelic_nrql_alert_condition resource at a later time. The newrelic_nrql_alert_condition resource is a future-facing resource and will become more of a standard across New Relic moving forward.

Hope this helps. Cheers :sunglasses:

1 Like