Follow the instructions to start the demo Codespace using the
nr-terraform-demo-codespaces
configuration and then follow along.
For local development we recommend using tfenv to manage your Terraform versions. tfenv
is already installed on this environment, we'll use it to install the latest stable version of terraform.
Learn how to install terraform using tfenv
View instructions
- List all the versions of Terraform that are available to install and scroll to the top of the list and find the most recent (highest version number number) stable (non alpha/beta) version using the
list-remote
command.
tfenv list-remote | head -n20
- Install the latest version (as found in the list above) using the
install
command:
tfenv install VERSION.NUMBER.HERE
e.g: tfenv install 1.7.5
- Switch to the installed version you want to use using the
use
command:
tfenv use VERSION.NUMBER.HERE
- Confirm terraform is installed correctly:
terraform -v
The "provider" defines what resources Terraform can manage. In this case we'll need to use the New Relic provider.
Important
You will need an UserAPI key and your New Relic account number to complete this step.
View instructions
Terraform configuration is provided via plain text files with the .tf
extension.
- View the provider documentation and copy the example provider block into a file called
provider.tf
- Update the provider block to include your account ID and API Key in the relevant positions.
- Initialise the Terraform configuration with the
init
command:
terraform init
In this exercise we'll use terraform to create an alert policy and add an alert condition to it.
View instructions
First create an alert policy. This is one of the most simple resources to configure.
- View the documentation for the alert policy resource
- Copy the example into a file called
alerts.tf
- Update the name of the resource to
example
- Use the documentation to set the incident preference of the policy to "One issue per condition"
- Set the name of the policy to "Example terraform alert policy" (this is how it will appear in New Relic)
It should look something like this:
resource "newrelic_alert_policy" "example" {
name = "Example Terraform alert policy"
incident_preference = "PER_CONDITION"
}
We need to add a condition to this policy. We will need to add the condition resource and then link it to the policy we configured previously.
- View the documentation for the nrql alert condition resource
- Copy from the first example the
newrelic_nrql_alert_condition
block (don't copy thenewrelic_alert_policy
we already have that!) - Change the name of the resource to
simple_nrql_condition
. - Add your account ID in the relevant position.
- Set the name of the condition to
Example NRQL condition
(this is the name as it appears in New Relic)
We can provide resources as inputs to other resources using dot notation. As you can see in the documentation for the nrql policy resource the resource exposes the id
of the policy. We can reference resource attribute as follows:
[resource_type].[resource_name].[attribute_name]
In this example the id
of the policy resource is:
newrelic_alert_policy.example.id
- Set the NRQL condition resource
policy_id
attribute to be the valuenewrelic_alert_policy.example.id
[!NOTE] Don't worry terraform knows what order to create things by building a dependency graph between the resources.
Terraform has a two stage commit plan
and apply
. You can use the plan
to see what changes would be made. By default the apply
will also run a plan
automatically.
- Run a plan using the
plan
command:
terraform plan
Review the changes the plan shows.
- Apply the changes:
terraform apply
(You need to respond yes
when prompted)
- Find the generated policy and condition in the New Relic user interface.
In this task we'll make some changes and see how the terraform responds.
- Make a change in the NRQL condition resource, for instance change the critical threshold to a different value.
- Apply the change and review the delta changes terraform intends to make:
terraform apply
- Confirm in the New Relic UI the change has been made
- In the New Relic UI make a change to the NRQL condition (change a threshold for instance)
- Run the terraform apply to and see that the change is noticed and reverted:
terraform apply
This exercise shows how resources can be generated from simple configuration. We will generate multiple synthetic monitors from a single, simple configuration.
View instructions
Before we create the synthetic monitor resource we need to create the configuration to drive it. This configuration can be passed in many ways, but to keep things simple we'll use terraform local variables. For this example our configuration will be a simple list of websites we'd like to check. We'll check two sites in this example, but you could add as many as you like.
- New Relic: https://www.newrelic.com
- BBC News: https://www.bbc.co.uk/news
For each site we need to specify the name of the site and the URL to check. We could of course supply all sorts of configuration attributes here, its entirely up to us. For example each site is like this:
{
name = "New Relic"
uri = "https://www.newrelic.com"
}
- Add the following configuration to a file called
synth.tf
. Configure it to your liking!
locals {
sites = {
relic = {
name = "New Relic"
uri = "https://www.newrelic.com"
},
bbc = {
name = "BBC"
uri = "https://www.bbc.co.uk/news"
}
}
}
You can see that both sites are added to the sites
object within locals
. We'll iterate over this to generate the configuration.
- Find the documentation for the synthetics monitor resource
- Copy the first example into the file
synth.tf
If we left it like this then we would get a single monitor. To generate a monitor for each site in our list we need to use the Terraform for_each feature.
- Immediately before the
status
attribute at the top of the resource definition add an attributefor_each
referencing our local variablelocal.sites
(note thes
is removed).
for_each = local.sites
For each iteration of the sites variable the object will become available in a special value called each
. This contains the key and value.
- Update the
name
attribute so that the site name is automatically added to the monitor name, extracting the name field from our site object. We use interpolation on the string to do this:
name = "Simple check: ${each.value.name}"
- Update the uri attribute to reference the
uri
value of the site object. As we're not appending any strings we can reference this directly:
uri = each.value.uri
- Apply the configuration
terraform apply
- Confirm that both synthetic monitors were created in the UI.
Complete the exercises by tidying up and deleting the resources you created.
Warning
Don't forget this step, otherwise you might incur charges for those synthetics!
View instructions
One of the nice things about using terraform is its easy to clean up after yourself! Now we're done with the exercise you can remove everything you created by running the destroy
command.
terraform destroy
Well done! You've learnt to install and use Terraform to manage New Relic resources.
If you want to learn more about Observability as Code with New Relic check out some of these resources: