“Terraform is a popular infrastructure-as-code software tool built by HashiCorp. You use it to provision all kinds of infrastructure and services, including New Relic dashboards and alerts.” - docs.newrelic.com
In this post I will create an Error
New Relic alert via Terraform, I see this Error Rate as Availability
and the resulting New Relic resources would be a provider
, alert policy
which is the parent, alert condition
which are children to the parent and alert trigger
Four Golden Signals
For your alerts you need to think about whats sensible to alert on, Goole SRE is the golden standard, because you know… its Google :D … so you can re-invent the wheel or learn from their Golden Signals:
- Error rate (Availability)
- Latency
- Traffic
- Saturation
High Level Commands & Flow
- terraform configuration (or clone existing), for new configuration the provider the minimum requirement
- terraform init
- terraform validate
- terraform plan
- terraform apply
Required Terraform Config
The Newrelic docs have a great example which I based the below on, you will notice they are also focused on the Goole SRE Golden Signals
The high level config that I used to create my .tf
files are
providers.tf
- creates
terraform
&provider
resources - optionally includes a backend which is just a space to keep the
terraform.tfstate
, Ive seen AWS S3 and Dynamo being used here- See Backend Initialization and Backend block configuration overview, the default is local
- creates
alerts.tf
- alert policy (this is the parent)
- alert conditions (these are the children)
- alert triggers
locals.tf
(optional)variables.tf
(optional)
Additional abstractions
Locals
Locals are like constants in terraform, example locals.tf
file with property newrelic_account_id
1 | locals { |
To access newrelic_account_id
in another file like providers.tf
1 | provider "newrelic" { |
Variables
1 | variable "environment" { |
I set the environment
in prod.tfvars, uat.tfvars
files, example value environment = "uat"
You can access the map newrelic_slack_channels
in a file, example locals.tf
1 | locals { |
Example Setup
- Install Terraform
Chocolatey makes this easy for windows users like me … sudo apt-get
this 🖕
1 | choco install terraform |
- Create your initial config with
providers.tf
andlocals.tf
providers.tf
1 | terraform { |
locals.tf
You can get this from your NR account under Administration
-> API Keys
1 | locals { |
- Run
terraform init
which will initialize the backend and provider plugins, mine created these files/folder locally. I manually modified mylocals.tf
with my account id and api key.
I then also updated my .gitignore with the config below
1 | .terraform/* |
- Create
Alert Policy
(parent) andAlert Condition
(children) insidealerts.tf
locals.tf
1 | // *** Alert policy |
- https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/nrql_alert_condition
- https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/infra_alert_condition
variables.tf
Additionally create variables.tf
1 | variable "environment" { |
Run
terraform validate
and fix any errors.Run
terraform plan
and specify the environment astest
Run
terraform apply
The console will confirm the actions
Additionall the local terraform.tfstate
file will be created
- Log in to New Relic and navigate to Alert Policies to confirm that Terraform created your new policy.
Alert Parent policy
Child Alert Condition
- Add triggers, the example below is of type
EMAIL
, others exist likeSLACK
trigger.tf
1 | resource "newrelic_notification_destination" "team_email_destination" { |
Run
terraform validate
and fix any errors.Run
terraform plan
and specify the environment astest
Run
terraform apply
You will notice the state updates and it creates a backup file for its internal process
Then the notification can be seen under the policy
Synthetic Monitor
This would be
1 | newrelic_alert_condition |