Dynamic widget for datadog

I want to created a datadog dashboard module to add widgets dynamically from my terraform.tfvars values.

resource "datadog_dashboard" "dashboard" {
  title = var.dashboard_title
  description = var.description
  layout_type = var.layout_type
  is_read_only = var.read_only

    dynamic widget {
      for_each = var.widgets
      content {
        type = widget.value.widget_type
        title = widget.value.widget_title
      }
      }
    }

This doesn’t seem to work and i get the error “An argument named “title” is not expected here”.

How would i got about achieving this?

Hi @jpashby,

I do believe that you are forgetting the keyworrd which indicates the kind of widget{}.

Here: https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/dashboard

Widget examples are using that syntax:

  widget {
    alert_value_definition {
      alert_id   = "895605"
      precision  = 3
      unit       = "b"
      text_align = "center"
      title      = "Widget Title"
    }
  }

You may add alert_value_definition{} block surrounding title and other stuff.

Cheers,

mathias

Thanks @mathias213. Problem is i want to have different types of widgets that is pulled from .tfvars. So for example alert_value_definition i want to be passed in from .tfvars

Something like that:

    dynamic widget {
      for_each = var.widgets
        content {
          widget.value.kind_of_widget {
            type = widget.value.widget_type
            title = widget.value.widget_title
          }
        }
      }
    }

Maybe?

Due to how this provider’s schema is designed, there’s no way to write a single generic block that covers all of the possible nested block types. Instead, I think you’ll need to write out a definition for each of the block types you might want to generate and for each one write block content that is valid for that particular block type (since each of them has a separate content schema).

I’m not familiar with this provider at all, so I’m relying only on what’s in the documentation, but it looks like they have a separate datadog_dashboard_json resource type which allows specifying the dashboard configuration as a single JSON string that the provider presumably validates using its own internal rules, rather than asking Terraform Core to enforce a particular schema itself. Given that, you may be able to more easily get the result you were hoping for by dynamically constructing a JSON value instead. For example:

resource "datadog_dashboard_json" "example" {
  dashboard = jsonencode({
    title       = var.dashboard_title
    description = var.description
    # ...

    widgets = var.widgets
  })
}

The above assumes that var.widgets has a type which jsonencode will convert into the JSON structure that this widgets property expects. If not, you may need to transform it using a for expression.

Depending on how this other resource type is implemented in the provider, this might mean that the dashboard isn’t validated as strongly at planning time as it would be for the datadog_dashboard resource type (for example, if it’s relying only on the remote API for some parts of its validation), but in return you get the flexibility of constructing this data structure however you like because from Terraform’s own perspective this is just a big string it knows nothing about.