Trying to use a dynamic block in Terraform to create multiple of the same widget in Datadog

807 Views Asked by At
resource "datadog_dashboard" "ordered_dashboard" {


  title        = "SLO Dashboard"
  description  = "Created using the Datadog provider in Terraform"
  layout_type  = "ordered"
  is_read_only = true


  dynamic widget {
    for_each = { for index, svc in local.list_of_services : svc.name => svc }
    content {
      widget_type = "group_definition"
      layout_type = "ordered"
      title       = "${each.value.name}"
      background_color = "vivid_green"
    }
  }
}

I eventually want to have more in each widget but right now I am just trying to get the dynamic block working with the Group definition widget. I have N number of svcs that I want terraform to create 2 widgets in a dashboard for each svc within groups I define.

I am getting widget type, layout type, title, and background color, arent expected here.

1

There are 1 best solutions below

0
On

The group_definition block must exists under widget.group_definition as stated in the Terraform documentation. In order to create N numbers of groups definitions per N numbers of services change your code to this:

resource "datadog_dashboard" "ordered_dashboard" {
  title        = "SLO Dashboard"
  description  = "Created using the Datadog provider in Terraform"
  layout_type  = "ordered"
  is_read_only = true


  dynamic "widget" {
    for_each = { for svc in local.list_of_services : svc.name => svc }
    content {
      group_definition {
        layout_type      = "ordered"
        title            = widget.value.name
        background_color = "vivid_green"
      }
    }
  }
}

This will create a a group definition per service defined in locals.list_of_services.

If you want to create N numbers of group_definition per service, you can also use the flatten function but you'll need to update the structure of your locals or variable, example:

locals {
  list_of_services = [
    {
      name = "svc_1"
      group_definition = [
        {
          title = "svc_1_title_1"
        },
        {
          title = "svc_1_title_2"
        }
      ]
    },
    {
      name = "svc_2"
      group_definition = [
        {
          title = "svc_2_title_3"
        },
        {
          title = "svc_2_title_4"
        }
      ]
    }
  ]

  group_definition = flatten([
    for svc_key, service in local.list_of_services : [
      for group_key, group_definition in local.list_of_services[svc_key].group_definition : {
        svc_key   = svc_key
        group_key = group_key
        title     = group_definition.title 
      }
    ]
  ])
}

resource "datadog_dashboard" "ordered_dashboard" {
  title        = "SLO Dashboard"
  description  = "Created using the Datadog provider in Terraform"
  layout_type  = "ordered"
  is_read_only = true


  dynamic "widget" {
    for_each = { for group_definition in local.group_definition : "${group_definition.svc_key}.${group_definition.group_key}" => group_definition }
    content {
      group_definition {
        layout_type      = "ordered"
        title            = widget.value.title
        background_color = "vivid_green"
      }
    }
  }
}