Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/grafana-cloud/learn-and-build/as-code/terraform/terraform-self-managed.md, or by sending Accept: text/markdown to https://grafana.com/docs/grafana-cloud/learn-and-build/as-code/terraform/terraform-self-managed/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
Manage a self-managed Grafana instance using Terraform
Learn how to configure the Grafana Terraform provider for a self-managed Grafana OSS or Grafana Enterprise instance, and add a data source, a folder, and a dashboard to it.
Note
The Grafana Terraform provider is updated frequently. For the latest provider version and the complete reference for every resource and data source, refer to the Grafana provider in the Terraform Registry.
Before you begin
Before you begin, ensure you have the following:
- A running Grafana OSS or Grafana Enterprise instance, and a user with the Admin organization role in it.
- Terraform installed on your machine.
- The JSON model of a dashboard that you want to provision. To export one from Grafana, refer to Export a dashboard as code.
Note
Save all the Terraform configuration files in the same directory.
Create a service account token
Terraform authenticates to Grafana with a service account token. To create one, complete the following steps:
Assign the service account the Admin role, so that it can create data sources, folders, and dashboards.
For production use, assign only the roles that the resources you manage need. Refer to Roles and permissions.
Add a token to the service account, and save the token for the next step.
Configure the provider
Create a file named provider.tf and add the following:
terraform {
required_providers {
grafana = {
source = "grafana/grafana"
version = "~> 4.0"
}
}
}
# Reads credentials from the GRAFANA_URL and GRAFANA_AUTH environment variables
provider "grafana" {}To keep your token out of source control, set the credentials as environment variables instead of adding them to the configuration:
export GRAFANA_URL="<GRAFANA_URL>"
export GRAFANA_AUTH="<SERVICE_ACCOUNT_TOKEN>"Replace the following placeholders:
<GRAFANA_URL>: The base URL of your Grafana instance, for examplehttp://localhost:3000.<SERVICE_ACCOUNT_TOKEN>: The service account token you created.
For other authentication options, refer to the provider authentication documentation.
Add a data source
This guide uses the Prometheus data source. The required arguments for grafana_data_source vary depending on the type of data source.
Create a file named datasource.tf and add the following:
resource "grafana_data_source" "prometheus" {
type = "prometheus"
name = "<DATA_SOURCE_NAME>"
url = "<PROMETHEUS_URL>"
}Replace the following placeholders:
<DATA_SOURCE_NAME>: The name of the data source in Grafana.<PROMETHEUS_URL>: The URL of your Prometheus server, for examplehttp://prometheus:9090.
Add a folder
Create a file named folder.tf and add the following grafana_folder resource:
resource "grafana_folder" "my_folder" {
title = "<FOLDER_TITLE>"
}Replace <FOLDER_TITLE> with a name for the folder.
Add a dashboard to the folder
Create a file named dashboard.tf and add the following grafana_dashboard resource:
resource "grafana_dashboard" "my_dashboard" {
config_json = file("<DASHBOARD_FILE>.json")
folder = grafana_folder.my_folder.uid
}Replace <DASHBOARD_FILE> with the name of the JSON file that contains the dashboard model.
Apply the Terraform configuration
In a terminal, run the following commands from the directory that contains the configuration files.
Initialize the working directory and install the Grafana provider:
shterraform initPreview the changes that Terraform makes:
shterraform planApply the configuration:
shterraform apply
Verify the resources
After Terraform applies the configuration, check the following in Grafana:
- The data source appears in Connections > Data sources.
- The folder appears in Dashboards.
- The dashboard appears inside the folder.
Next steps
You created a data source, a folder, and a dashboard in a self-managed Grafana instance using Terraform. To continue:
- Manage dashboards from a Git repository with CI/CD. Refer to Create and manage dashboards using Terraform and GitHub Actions.
- Provision alerting resources. Refer to Use Terraform to provision alerting resources.
- Explore all available resources in the Grafana provider documentation.
Was this page helpful?
Related resources from Grafana Labs


