Terraform is a powerful observability-as-code tool that helps you define and manage your cloud and on-prem resources through configuration files that use HCL (HashiCorp Configuration Language). However, it can be challenging to work with HCL syntax, and that can make it difficult to use Terraform to update and add new widgets to an existing dashboard in New Relic.
Fortunately, you can use JSON templates to make it easier to manage your Terraform-provisioned New Relic dashboards without having to convert them to HCL syntax. With JSON templates, you can make all your edits in the New Relic UI, then export the configuration into your Terraform project. These JSON templates also allow you to use the extended features of New Relic dashboards, including series coloring and the ability to specify the scale of the y-axis.
In this blog, you’ll learn how to create a New Relic dashboard in Terraform using JSON templates. This is part one in a three-part series.
- In part two, Dynamically creating New Relic dashboards with Terraform, you'll learn how to use Terraform with JSON and template variables to dynamically generate New Relic dashboards.
- In part three, Using Terraform to generate New Relic dashboards from NRQL queries, you'll learn how to use Terraform with NRQL queries to generate a dynamic New Relic dashboard based on the result of a query. This is really useful for creating dashboards that show data like the five slowest HTTP requests and so on.
Prerequisites
Note: To create New Relic dashboards in Terraform using JSON templates, you need to be using provider version 3.4.0 or above.
To follow along with the sample code in this tutorial, you’ll need to have Terraform installed. For instructions for installing Terraform and guidance on how to run the examples, see the Github repository nr-terraform-json-dashboard-examples.
Creating a dashboard using JSON
First, get the JSON configuration of any dashboard in New Relic. The dashboard you choose doesn’t matter—you just need the JSON for this tutorial. Find an existing dashboard or create a new one using the New Relic UI and select the Copy JSON to clipboard icon in the top right of the dashboard.
Create a directory in your Terraform project called dashboards and paste the JSON from your clipboard into a new file called dashboard.json
. You should update the first "name"
attribute in dashboard.json
in order to distinguish it from the dashboard you used as a source.
You then need to set up your Terraform configuration to use this dashboard.json
as an input. To do that create a file called dash_basic.tf
and enter the following HCL code:
resource "newrelic_one_dashboard_json" "basic_dashboard" {
json = file("${path.module}/dashboards/dashboard.json")
}
resource "newrelic_entity_tags" "basic_dashoard" {
guid = newrelic_one_dashboard_json.basic_dashboard.guid
tag {
key = "terraform"
values = [true]
}
}
output "basic_dashboard" {
value = newrelic_one_dashboard_json.basic_dashboard.permalink
}
The first block passes dashboard.json
to the newrelic_one_dashboard_json
resource.
The second block tags the dashboard so you know it's Terraform-managed, which is a best practice. In this case, the value of the tag is:
tag {
key = "terraform"
values = [true]
}
Finally, the third block outputs the dashboard permalink to the console so it's easy to find.
Apply your Terraform and confirm the dashboard is created as expected.
Updating a dashboard
It’s simple enough to create a dashboard but how should you approach updates? Let’s take a look.
Single-use dashboards
This flow chart shows the process for maintaining a single-use dashboard.
Simply edit the dashboard in the New Relic UI, copy the new JSON configuration to your clipboard, update the dashboard.json file
in your project, and reapply your Terraform.
Multi-use dashboards
What if you want to do more with your dashboards? For example, you might want to take a template dashboard and deploy it multiple times with some contextual changes in each dashboard.
One solution is to create a ‘template’ dashboard that you maintain in the New Relic UI. When you want to change your deployed dashboards, you edit the template dashboard, copy the JSON into your dashboard.json
file, and then reapply.
Here’s a flow chart that details what that process looks like.
If you’re only making small changes to each dashboard that's deployed, such as updating an appName
or account ID, then you can use the Terraform replace()
function to switch out values in the source template for new values in the deployed dashboards.
For example, the next Terraform code example for dash_replacer.tf
changes the dashboard name and sets the account ID:
resource "newrelic_one_dashboard_json" "replacer_dashboard" {
json = replace(
replace( # This changes the dashboard name
file("./dashboards/dashboard.json"),
"by Terraform"
,"renamed by Terraform"
),
"1234567", # This is the value in the source file
"2233445" # This is the value it will be changed to
)
}
The nice thing about this approach is that anytime you need to update a dashboard, you can simply edit the template dashboard, copy and paste your JSON into the template file, and redeploy.
If you want to make many changes in a template at deployment time, you can use the Terraform templatefile
which gives you better control of your changes. However, you’ll have to update your configuration so that it’s compatible with the Terraform templatefile
. The dash_templatefile.tf
file in the example repo shows an example of this in action.
As you can see, it’s fairly simple to create new dashboards this way—or to redeploy dashboards with a few minor tweaks. But what if you want to do something more complex, such as dynamically creating a dashboard? You’ll learn how to do that in part two of this series.
다음 단계
- Read part two of the series, Dynamically creating New Relic dashboards with Terraform, to learn how to use Terraform with JSON and template variables to dynamically generate New Relic dashboards.
- Read part three of the series, Using Terraform to generate New Relic dashboards from NRQL queries, to learn how to use Terraform with NRQL queries to generate a dynamic New Relic dashboard based on the result of a query.
If you’re not using New Relic yet, get started with New Relic for free. Your free account includes 100 GB/month of free data ingest, one free full-access user, and unlimited free basic users.
이 블로그에 표현된 견해는 저자의 견해이며 반드시 New Relic의 견해를 반영하는 것은 아닙니다. 저자가 제공하는 모든 솔루션은 환경에 따라 다르며 New Relic에서 제공하는 상용 솔루션이나 지원의 일부가 아닙니다. 이 블로그 게시물과 관련된 질문 및 지원이 필요한 경우 Explorers Hub(discuss.newrelic.com)에서만 참여하십시오. 이 블로그에는 타사 사이트의 콘텐츠에 대한 링크가 포함될 수 있습니다. 이러한 링크를 제공함으로써 New Relic은 해당 사이트에서 사용할 수 있는 정보, 보기 또는 제품을 채택, 보증, 승인 또는 보증하지 않습니다.