Use AWS Function Name in NRQL Alert Condition | Terraform

Hi Team,

So I am trying to create an Alarm based on a query, which queries my AWS lambda logs.

I want to add the AWS Lambda name to the Name of the resource. The reason is that I have a lot of lambdas and I want to make this more dynamic.

Eg: resource “newrelic_nrql_alert_condition” “opcua_retry_connection_loop” {
policy_id = newrelic_alert_policy.apm_alert_policy.id
type = “static”
name = “${provider.functionName} it’s stuck in retrying to connect”
description = “${provider.functionName} Alert when it’s stuck in retrying to connect”
enabled = true
value_function = “sum”
violation_time_limit_seconds = 86400

aggregation_window = 60

nrql {
query = “SELECT count(*) FROM Log WHERE message LIKE ‘%still trying to connect%’”
evaluation_offset = 1
}

critical {
operator = “above”
threshold = 40
threshold_duration = 600
threshold_occurrences = “AT_LEAST_ONCE”
}

warning {
operator = “above”
threshold = 20
threshold_duration = 600
threshold_occurrences = “AT_LEAST_ONCE”
}
}

But this does not work because is not able to find provider.functionName.

Any suggestion?

Additional information, my aws lambdas are running in AWS IoT Greengrass.

It would be nice if in terraform we could fetch tag dynamic and add them to alarm description and name like we can do with Alert custom violation descriptions | New Relic Documentation

So in short: I am trying to create an Alarm over our logs using Terraform NewRelic Provider. As you know, in production we will reach around 200 AWS Lambdas + 300 servers.

We need to dynamically add AWS lambda to the alarm description. I am trying to do it through Terraform, and currently, i don’t see a way to do it (manually through GUI is possible).

this could be useful if it works…but it does not :frowning:

data "newrelic_entity" "aws_lambda" {
  name = "aws_lambda"
  domain = "INFRA"
  type = "AWSLAMBDAFUNCTION"
  tag {
    aws {
      key: "functionName"
    }
  }
}

Never mind… solved by doing this:

resource "newrelic_nrql_alert_condition" "healthcheck_failed" {
  policy_id                    = newrelic_alert_policy.apm_alert_policy.id
  type                         = "static"
  name                         = "To many HealthCheck Failed errors"
  description                  = "{{targetName}} have to many HealthCheck failed errors"
  enabled                      = true
  value_function               = "sum"
  violation_time_limit_seconds = 86400
  expiration_duration            = 300
  close_violations_on_expiration = true
  aggregation_window             = 60

  nrql {
    query             = "SELECT count(*) FROM Log WHERE message LIKE '%Error: HealthCheck failed%' FACET aws.logGroup"
    evaluation_offset = 1
}

  critical {
    operator              = "above"
    threshold             = 8
    threshold_duration    = 600
    threshold_occurrences = "AT_LEAST_ONCE"
  }

  warning {
    operator              = "above"
    threshold             = 5
    threshold_duration    = 600
    threshold_occurrences = "AT_LEAST_ONCE"
  }
}

Have a good weekend!