Observability as Code: Managing New Relic With Terraform
Click-configured alerts drift, get deleted, and differ between environments. Observability as code with Terraform makes New Relic configuration versioned, reviewable, and repeatable.
Provider Setup
terraform {
required_providers {
newrelic = { source = "newrelic/newrelic" }
}
}
provider "newrelic" {
account_id = var.nr_account_id
api_key = var.nr_api_key
region = "US"
}
Alert Policy and Condition
resource "newrelic_alert_policy" "checkout" {
name = "Checkout - Production"
}
resource "newrelic_nrql_alert_condition" "error_rate" {
policy_id = newrelic_alert_policy.checkout.id
name = "High error rate"
nrql {
query = "SELECT percentage(count(*), WHERE error IS true) FROM Transaction WHERE appName = 'checkout-api'"
}
critical {
operator = "above"
threshold = 5
threshold_duration = 300
threshold_occurrences = "all"
}
}
What to Manage as Code
- Alert policies, conditions, and workflows
- Notification destinations
- Dashboards (
newrelic_one_dashboard) - Synthetic monitors
- Workloads and service levels
- Tags
Modules
Create a module like service-observability that gives every new service a standard set of alerts, a dashboard, and an SLO — passing in the service name.
CI/CD
terraform planon pull requests — reviewers see exactly what changesterraform applyon merge- Separate state per environment
Tips
- Import existing resources gradually
- Don't mix UI and Terraform edits for the same resources
- Keep secrets out of code
When observability is code, every new service gets good monitoring by default.