How to upload dashboards as .json files to kubernetes via helm

I’m using Terraform and I found a way to upload multiple dashboards building on @rafzukow 's answer.

Create a folder with the json files in it at the root of the Terraform module - I called it “custom_dashboards”.

Make the values file a Terraform template with a .tftpl suffix.

Use the helm_release provider to create the installation - I used kube-prometheus-stack with grafana as a subchart:

resource "helm_release" "monitoring" {
  name  = "prometheus"
  repository = "https://prometheus-community.github.io/helm-charts"
  chart = "kube-prometheus-stack"
  namespace = "monitoring"
  values  = [templatefile("${path.module}/helm/prometheus-values.tftpl", {
    custom_dashboards = fileset("${path.module}/custom_dashboards/", "*.json"),
    module_path = path.module
  })]
}

Then in the template file under the Grafana values under the dashboardProviders key:
prometheus-values.tftpl

  dashboardProviders:
    dashboardproviders.yaml:
      apiVersion: 1
      providers:
      - name: 'default'
        orgId: 1
        folder: ''
        type: file
        disableDeletion: true
        editable: true
        options:
          path: /var/lib/grafana/dashboards/default
  dashboards:
    default:
      %{ for dashboard in custom_dashboards ~}
${indent(4, replace(replace(dashboard, ".json", ""), "./", ""))}:
        json: |
          ${indent(8, file("${module_path}/custom_dashboards/${dashboard}"))}
      %{ endfor }

This way Terraform will dynamically pick up the json files in the directory and load them into the values file. I left folder blank since I want them to go into my General folder, however it should work if you specify a value there to get them in a separate folder.

2 Likes