Working with New Relic (newrelic nrql) and Terraform in Unix/Linux

New Relic is a web application, aimed primarily at developers. Its main objective is to track the ways users interact with designed products. This tool analysts.

Key features of New Relic:

  • Monitoring of the availability.
  • Warning and notification.
  • Performance profiles.
  • Plugins.
  • Histograms and percentiles.
  • The API access.
  • Response time in real-time.
  • The performance analyzer of the Java virtual machine.
  • Error detection, analysis.
  • The database call response time and throughput.
  • Diagnostics, transaction tracing and the stack trace.
  • Analysis of response time, throughput and breakdown by components.
  • Details on SQL performance.
  • Analysis of the user in real-time when browsing web pages.
  • Custom dashbord.
  • Track individual business transactions.
  • X-Ray session for business transactions.
  • Cross-application tracing for distributed applications.
  • Reports on availability, scalability, deployment.

Install terraform in Unix/Linux

The installation is very primitive and I described how to do this here:

Install terraform in Unix/Linux

So, in this article, I created a script to automatically install the software. It has been tested on CentOS 6/7, Debian 8, and on Mac OS X. Everything is working properly!

To get help on commands, run:

$ terraform --help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
 apply Builds or changes infrastructure
 console Interactive console for Terraform interpolations
 destroy Destroy Terraform-managed infrastructure
 env Workspace management
 fmt Rewrites config files to canonical format
 get Download and install modules for the configuration
 graph Create a visual graph of Terraform resources
 import Import existing infrastructure into Terraform
 init Initialize a Terraform working directory
 output Read an output from a state file
 plan Generate and show an execution plan
 providers Prints a tree of the providers used in the configuration
 push Upload this Terraform module to Atlas to run
 refresh Update local state file against real resources
 show Inspect Terraform state or plan
 taint Manually mark a resource for recreation
 untaint Manually unmark a resource as tainted
 validate Validates the Terraform files
 version Prints the Terraform version
 workspace management Workspace

All other commands:
 debug Debug output management (experimental)
 force-unlock Manually unlock the terraform state
 state Advanced state management

Start using!

Working with New Relic (newrelic nrql) and Terraform in Unix/Linux

I have the terraform folder, in it I will lay providers with whom I work. Because in this example I will use newrelic, it will create the folder and move into it. Further, in this folder, you should create:

$ mkdir examples modules

In the examples folder, I keep so called “playbook” to razvarivaya various services, for example — zabbix-server, grafana, web servers, and so on. In modules directory I will store all the necessary modules.

Can start writing the module, but for this task, I will create a folder:

$ mkdir modules/newrelic_nrql

Go to it:

$ cd modules/newrelic_nrql

Open the file:

$ vim

In this file, insert:

# Add newrelic nrql alert condition
resource "newrelic_nrql_alert_condition" "nrql_alert_condition" {
 count = "${var.nrql_alert_condition ? 1 : 0}"

 name = "${var.nrql_alert_condition_name !="" ? var.nrql_alert_condition_name : "${lower(}-nr-nrql-alert-condition-${lower(var.environment)}" }"
 policy_id = "${var.nrql_alert_condition_policy_id}"

 runbook_url = "${var.nrql_alert_condition_runbook_url}"
 enabled = "${var.nrql_alert_condition_enabled}"
 value_function = "${var.nrql_alert_condition_value_function}"

 term {
 duration = "${var.nrql_alert_condition_term_duration}"
 operator = "${var.nrql_alert_condition_term_operator}"
 priority = "${var.nrql_alert_condition_term_priority}"
 threshold = "${var.nrql_alert_condition_term_threshold}"
 time_function = "${var.nrql_alert_condition_term_time_function}"

 # term {
 # duration = 5
 # operator = "below"
 # priority = "warning"
 # threshold = "1"
 # time_function = "all"
 # }

 nrql {
 query = "${var.nrql_alert_condition_nrql_query}"
 since_value = "${var.nrql_alert_condition_nrql_since_value}"

 lifecycle = {
 create_before_destroy = true,
 ignore_changes = []

 depends_on = []


Open the file:

$ vim

And write:

# Global 
variable "name" {
 description = "The name for newrelic_alert resources"
 default = "test"

variable "environment" {
 description = "environment"
 default = "prod"

# newrelic_nrql_alert_condition
variable "nrql_alert_condition" {
 description = "Enable newrelic_nrql_alert_condition"
 default = "false"

variable "nrql_alert_condition_name" {
 description = "(Required) The title of The condition"
 default = ""

variable "nrql_alert_condition_policy_id" {
 description = "(Required) The ID of the policy where this condition should be used."
 default = ""

variable "nrql_alert_condition_runbook_url" {
 description = "(Optional) Runbook URL to display in notifications."
 default = ""

variable "nrql_alert_condition_enabled" {
 description = "(Optional) Set whether to enable the alert condition. Defaults to true."
 default = "true"

variable "nrql_alert_condition_value_function" {
 description = "(Optional) Possible values are single_value sum."
 default = "single_value"

variable "nrql_alert_condition_term_duration" {
 description = "(Required) In minutes, must be: 1, 2, 3, 4, 5, 10, 15, 30, 60, or 120."
 default = 5

variable "nrql_alert_condition_term_operator" {
 description = "(Optional) above, below, or equal. Defaults to equal."
 default = "equal"

variable "nrql_alert_condition_term_priority" {
 description = "(Optional) critical or warning. Defaults to critical."
 default = "critical"

variable "nrql_alert_condition_term_threshold" {
 description = "(Required) Must be 0 or greater."
 default = 0

variable "nrql_alert_condition_term_time_function" {
 description = "(Required) all or any."
 default = "all"

variable "nrql_alert_condition_nrql_query" {
 description = "(Required) The NRQL query to execute for the condition."
 default = ""

variable "nrql_alert_condition_nrql_since_value" {
 description = "(Required) The value to be used in the SINCE <X> MINUTES AGO clause for the NRQL query. Must be: 1, 2, 3, 4, or 5."
 default = 1

Actually in this file are stored all the variables. Thanks cap!

Open the last file:

$ vim

And need to insert the following lines:

# newrelic_nrql_alert_condition
output "nrql_alert_condition_id" {
 description = "ID for newrelic_nrql_alert_condition"
 value = "${newrelic_nrql_alert_condition.nrql_alert_condition.*.id}"

Now go to the folder aws/examples and create another folder to check is written of a miracle:

$ mkdir newrelic_nrql && cd $_

Inside the folders you created open the file:

$ vim

And paste the following code into it:

# MAINTAINER: Vitaliy Natarov "[email protected]"
terraform {
 required_version = "~> 0.11.11"

provider "newrelic" {
 api_key = "75e6741e6326cce1666ecfb94c3c0b8fdf"

module "newrelic_alert" {
 source = "../../modules/newrelic_alert"

 # vars for newrelic_alert_policy
 alert_policy = "true"
 #alert_policy_name = "new-relic-policy-PER_CONDITION"
 alert_policy_incident_preference = "PER_CONDITION"
 alert_policy_simple_default = "true"

 alert_channel = "true"
 alert_channel_slack = "true"
 alert_channel_slack_configuration_channel = "new-relic"
 alert_channel_slack_configuration_url = ""
 alert_condition = "true"
 alert_condition_policy_id = "${element(module.newrelic_alert.alert_policy_id, 0)}"
 #alert_condition_policy_id = "${element(module.newrelic_alert.simple_default_alert_policy_id, 0)}"
 alert_condition_type = "apm_app_metric"
 alert_condition_entities = ["PHP Application"]

 alert_policy_channel = "false"

module "newrelic_nrql" {
 source = "../../modules/newrelic_nrql"

 nrql_alert_condition = "true"
 nrql_alert_condition_policy_id = "${element(module.newrelic_alert.alert_policy_id, 0)}"

 nrql_alert_condition_nrql_query = "SELECT count(*) FROM SyntheticCheck WHERE monitorId = '1'"


In the file is to register all needed, but most importantly:

  • api_key — the API key which you can generate in “‘Account settings’->’API keys'”.

Everything is already written and ready to use. Well, let’s start testing. In the folder with your playboycom performed:

$ terraform init

This action, I initialize the project. Then, tighten the module:

$ terraform get

PS: To update the changes in the module itself, you can run:

$ terraform get-update

Check the validation:

$ terraform validate

Zapuskaem run:

$ terraform plan

I brought everything I have is good and you can run the deployment:

$ terraform apply

As you can see from the output, everything went smoothly! To remove creation, you can run:

$ terraform destroy

Another utility:

Working with New Relic (newrelic alert) and Terraform in Unix/Linux

All material Uploader in github account for easy use:

$ git clone

But that’s about it. This article “Working with New Relic (newrelic nrql) and Terraform in Unix/Linux” is completed.


(Visited 92 times, 1 visits today)