Add the dashboard resource

The grafana_dashboard resource tells Terraform to create or update a dashboard in Grafana using a JSON file. You point it at the file your Foundation SDK code generated (or any valid dashboard JSON), and Terraform handles the rest.

To add the dashboard resource, complete the following steps:

  1. Copy your dashboard.json file into the terraform directory (or adjust the path in the next step to point to where it lives):

    Bash
    cp ../dashboard.json .
  2. Create a file named dashboards.tf with a folder and dashboard resource:

    hcl
    resource "grafana_folder" "dashboards_as_code" {
      title = "Dashboards as Code"
    }
    
    resource "grafana_dashboard" "my_dashboard" {
      config_json = file("${path.module}/dashboard.json")
      folder      = grafana_folder.dashboards_as_code.id
    }

    This configuration:

    • Creates a folder named “Dashboards as Code” in Grafana
    • Deploys your dashboard JSON into that folder

Deploying multiple dashboards

If you have multiple JSON files, use Terraform’s for_each to deploy them all:

hcl
resource "grafana_dashboard" "all" {
  for_each    = fileset("${path.module}/dashboards/", "*.json")
  config_json = file("${path.module}/dashboards/${each.key}")
  folder      = grafana_folder.dashboards_as_code.id
}

Add a new JSON file to the dashboards/ directory, run terraform apply, and the dashboard appears in Grafana.

In the next milestone, you run terraform plan to preview what Terraform will do before applying any changes.


page 5 of 8