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:
Copy your
dashboard.jsonfile into theterraformdirectory (or adjust the path in the next step to point to where it lives):cp ../dashboard.json .Create a file named
dashboards.tfwith a folder and dashboard resource: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:
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.