Search code examples
terraformnewrelic

Using template variables in a new relic terraformed widget


So I am using the new relic terraform provider to generate a dashboard. I want this dashboard to be dynamic, it has a variable with a list of domains and then this variable is used in the subsequent queries. For example

SELECT * FROM Span WHERE service = {{ domain }}

Assuming variable is labeled as "domain"

This works fine if I create is using the new relic GUI. The issue arrises when I attempt to generate the dashboard via terraform. I can create the variable and populate it on my dashboard just fine. But the syntax around using the variable in the queries, the {{ domain }} throws an error.

Error: query error; Invalid widget input with id 163128331: error parsing 'SELECT uniques(`client`) FROM Span WHERE `entity.name` = 'supergraph-prod' AND service = {{ domain }}'', please check nrql grammar.
│ Cause: Invalid nrql query.
│
│   with module.dashboard-federation.newrelic_one_dashboard.dashboard[0],
│   on .terraform/modules/dashboard-federation/dashboard/main.tf line 2, in resource "newrelic_one_dashboard" "dashboard":
│    2: resource "newrelic_one_dashboard" "dashboard" {

Hopefully I am just missing the correct syntax. Yet I can't seem to find an example relating to my issue online. Any help would be appreciate.

The way teh widget is created is by passing an object to a dynamic widget creator that maps the values into their correct places. Here is my object:

{
      title       = "Consumers"
      widget_type = "widget_table"
      query       = "SELECT uniques(client) FROM Span WHERE `entity.name` = '${var.newrelic_app_name}' AND service = {{ domain }}'"
      row         = 1
      column      = 1
    },
reasource "newrelic_one_dashboard" "dashboard" {
 dynamic "widget_table" {
        for_each = page.value.widget_table
        iterator = widget
        content {
          title                    = try(widget.value.title, widget.value.query)
          filter_current_dashboard = try(widget.value.filter_current_dashboard, false)
          row                      = try(widget.value.row, 0)
          column                   = try(widget.value.column, 0)
          width                    = try(widget.value.widget_width, 4)
          height                   = try(widget.value.widget_height, 3)

          nrql_query {
            query = widget.value.query
          }
        }
      }
}

Thanks :)


Solution

  • Variables should be fine both with this resource and the json version. In your query you have a mistake I think, there is an extra single quote right at the end that needs to be removed

    query       = "SELECT uniques(client) FROM Span WHERE `entity.name` = '${var.newrelic_app_name}' AND service = {{ domain }}'"
    

    should instead read:

    query       = "SELECT uniques(client) FROM Span WHERE `entity.name` = '${var.newrelic_app_name}' AND service = {{ domain }}"
    

    (note the final single quote before the closing double quote)

    Also you may consider using the attribute IN ({{domain}}) syntax to better support multi-select.