Provision cross-dashboard variables using Terraform
Use the Grafana Terraform provider to manage cross-dashboard variables as code. Cross-dashboard variables are available to multiple dashboards, either globally across the organization or to the dashboards in a single folder. Managing them as code lets you version-control the variables and keep them consistent across Grafana instances.
This guide outlines the steps to provision cross-dashboard variables with Terraform:
- Configure the Grafana Terraform provider.
- Define your global and folder-scoped variables in Terraform.
- Run
terraform applyto provision the variables. - Opt your dashboards in to the variables, either in the Grafana UI or with Terraform.
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:
- Grafana instance: A Grafana instance or Grafana Cloud stack.
- Permissions: The Admin role to create global variables, or the Editor role with Edit permission on the folder to create folder-scoped variables.
- Terraform: Terraform 1.9 or later installed on your machine. For more information, refer to the Terraform install documentation.
- Grafana Terraform provider: Version 4.30.0 or later of
grafana/grafana, which adds thegrafana_apps_generic_resourceresource that this guide uses.
Note
Save all the Terraform configuration files in the same directory.
Configure the Grafana provider
Terraform authenticates against Grafana with a username and password or with a service account token. Use a service account token for Grafana Cloud.
Create a service account token
To create a service account token, follow these steps:
- Create a service account and assign it the Admin role, which is required to create global variables. To create a service account, refer to Create a service account.
- Add a token to the service account. To add a token, refer to Add a token to a service account.
- Copy the token and store it securely, because you can’t view it again after you leave the page.
Create the provider configuration
Create a file named main.tf and add the following:
terraform {
required_version = ">= 1.9.0"
required_providers {
grafana = {
source = "grafana/grafana"
version = ">= 4.30.0"
}
}
}
variable "grafana_auth" {
type = string
sensitive = true
}
provider "grafana" {
url = "<GRAFANA_URL>"
auth = var.grafana_auth
# Required for Grafana OSS and Grafana Enterprise. Remove for Grafana Cloud.
org_id = 1
}Replace <GRAFANA_URL> with the root URL of your Grafana instance, without a trailing path, for example http://localhost:3000 or https://my-stack.grafana.net.
The grafana_auth variable holds the service account token, or <USERNAME>:<PASSWORD> for a self-managed instance. To keep credentials out of your configuration files, set the variable through the TF_VAR_grafana_auth environment variable when you apply the configuration.
You can also set the GRAFANA_URL and GRAFANA_AUTH environment variables, which override the provider block.
The org_id argument maps organization 1 to the default API namespace. In Grafana Cloud, omit org_id, because the grafana_apps_generic_resource resource discovers the stack namespace automatically.
Define cross-dashboard variables
The provider doesn’t have a typed resource for variables. Instead, use the generic App Platform resource, grafana_apps_generic_resource, to send the same Kubernetes-style manifest that Grafana stores, with apiVersion set to dashboard.grafana.app/v2beta1 and kind set to Variable.
The examples in this section create the following variables:
- A global constant variable,
$env. - A global custom variable,
$region. - A folder-scoped custom variable,
$cluster.
Create a file named variables.tf and add the examples from the following sections.
Define a global variable
Global variables are available to every opted-in dashboard in the organization or stack.
For global variables, metadata.name must equal spec.spec.name.
The following example creates a constant variable named env:
resource "grafana_apps_generic_resource" "env" {
manifest = {
apiVersion = "dashboard.grafana.app/v2beta1"
kind = "Variable"
metadata = {
name = "env"
}
spec = {
kind = "ConstantVariable"
spec = {
name = "env"
query = "prod"
}
}
}
}The following example creates a custom variable named region with three values:
resource "grafana_apps_generic_resource" "region" {
manifest = {
apiVersion = "dashboard.grafana.app/v2beta1"
kind = "Variable"
metadata = {
name = "region"
}
spec = {
kind = "CustomVariable"
spec = {
name = "region"
query = "us-east-1,us-west-2,eu-west-1"
current = { text = "us-east-1", value = "us-east-1" }
options = []
multi = false
includeAll = false
hide = "dontHide"
skipUrlSync = false
allowCustomValue = true
}
}
}
}Define a folder-scoped variable
Folder-scoped variables are available only to the dashboards in the same folder. For folder-scoped variables:
metadata.namemust be<VARIABLE_NAME>--<FOLDER_UID>.- The
grafana.app/folderannotation must be set to the UID of the folder. - The folder must already exist.
The following example creates a folder with the grafana_folder resource and a custom variable named cluster in that folder:
resource "grafana_folder" "shared" {
title = "Shared variables demo"
uid = "tf-cross-dash-vars"
}
resource "grafana_apps_generic_resource" "cluster" {
manifest = {
apiVersion = "dashboard.grafana.app/v2beta1"
kind = "Variable"
metadata = {
name = "cluster--tf-cross-dash-vars"
annotations = {
"grafana.app/folder" = grafana_folder.shared.uid
}
}
spec = {
kind = "CustomVariable"
spec = {
name = "cluster"
query = "prod,staging"
current = { text = "prod", value = "prod" }
options = []
multi = false
includeAll = false
hide = "dontHide"
skipUrlSync = false
allowCustomValue = true
}
}
}
depends_on = [grafana_folder.shared]
}Don’t set metadata.namespace in the manifest. The provider sets the namespace to default for organization 1 or stacks-<STACK_ID> for Grafana Cloud.
Load the manifest from a YAML file
Instead of writing the manifest in HCL, you can keep it in a YAML file and load it with the yamldecode() function.
The following env.yaml file defines the same variable as the env resource in Define a global variable:
apiVersion: dashboard.grafana.app/v2beta1
kind: Variable
metadata:
name: env
spec:
kind: ConstantVariable
spec:
name: env
query: prodReference the file from the resource:
resource "grafana_apps_generic_resource" "env" {
manifest = yamldecode(file("${path.module}/env.yaml"))
}Allow changes from the Grafana UI
By default, you can’t edit Terraform-managed variables in the Grafana UI. To allow UI edits, set allow_ui_updates to true on the resource:
resource "grafana_apps_generic_resource" "env" {
allow_ui_updates = true
manifest = { /* ... */ }
}Provision the variables
In a terminal, run the following commands from the directory where all of the configuration files are located.
Initialize the working directory, which downloads the Grafana provider.
terraform initSet your credentials, then preview the changes that Terraform makes.
export TF_VAR_grafana_auth='<TOKEN>' terraform planReplace
<TOKEN>with the service account token that you created in Create a service account token.Apply the configuration files.
terraform applyBefore applying any changes, Terraform displays the execution plan and requests your approval:
Plan: 4 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:After you enter
yes, Terraform creates the folder and the three variables:Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
To verify the result, go to Dashboards > Variables in Grafana.
The env and region variables are listed as global variables, and the cluster variable is listed in the Shared variables demo folder.
Opt a dashboard in to the variables
Dashboards don’t use cross-dashboard variables automatically. After you create the variables, opt each dashboard in to them.
Only dynamic dashboards (v2 schema) can use cross-dashboard variables. Folder-scoped variables are available only to dashboards in the same folder.
You can opt a dashboard in from the Grafana UI or with Terraform.
Opt in from the Grafana UI
To opt in a dashboard from the Grafana UI, refer to Add cross-dashboard variables to a dashboard.
Opt in with Terraform
A dashboard opts in to cross-dashboard variables through the grafana.app/useCrossDashboardVariables annotation.
The value of the annotation is a JSON object with a selection for each scope:
{
"global": "all" | "none" | ["env", "region"],
"folder": "all" | "none" | ["cluster"]
}The following table describes the possible values:
Setting both scopes to "none" is the same as omitting the annotation.
The typed grafana_apps_dashboard_dashboard_v2 resource exposes annotations as read-only, so it can’t set the annotation. Use the grafana_apps_generic_resource resource instead.
The following example creates a dashboard in the Shared variables demo folder that uses the env, region, and cluster variables:
resource "grafana_apps_generic_resource" "demo_dashboard" {
manifest = {
apiVersion = "dashboard.grafana.app/v2"
kind = "Dashboard"
metadata = {
name = "tf-cross-dash-vars-demo"
annotations = {
"grafana.app/folder" = grafana_folder.shared.uid
"grafana.app/useCrossDashboardVariables" = jsonencode({
global = ["env", "region"]
folder = ["cluster"]
})
}
}
spec = {
title = "Cross-dashboard variables demo"
cursorSync = "Off"
preload = false
elements = {}
layout = { kind = "GridLayout", spec = { items = [] } }
timeSettings = {
timezone = "browser"
from = "now-6h"
to = "now"
}
}
}
depends_on = [
grafana_apps_generic_resource.env,
grafana_apps_generic_resource.region,
grafana_apps_generic_resource.cluster,
]
}The dashboard.grafana.app/v2 API version requires Grafana 13 or later. On Grafana 12, set the dashboard apiVersion to dashboard.grafana.app/v2beta1. Variables always use dashboard.grafana.app/v2beta1.
After you apply the configuration, the $env, $region, and $cluster variables appear on the dashboard.
Cross-dashboard variables are added at runtime. You can use them in queries, but Grafana doesn’t write them to the dashboard JSON when you save the dashboard.
If the dashboard has a local variable with the same name as a cross-dashboard variable, the local variable takes precedence.
Update and delete variables
To update a variable, change its configuration and run terraform apply again. For example, if you change the query of the env variable from prod to staging, Terraform updates the variable in place.
You can’t change the name (spec.spec.name) or the folder scope of an existing variable.
To rename a variable or move it to another folder, remove the resource from your configuration and run terraform apply to delete the variable. Then, add a resource with the new name or scope and run terraform apply again.
To delete all the resources that Terraform manages in this directory, run the following command and enter yes to confirm:
terraform destroyThis command only removes the objects in the Terraform state of this directory, in this example the folder and the three variables.
Import existing variables
To bring an existing cross-dashboard variable under Terraform management, add a matching resource block to your configuration and import the variable:
terraform import grafana_apps_generic_resource.env \
dashboard.grafana.app/v2beta1/Variable/envTroubleshoot
The following table lists common errors and their likely causes:
Use Crossplane
If you use Crossplane, the Grafana Crossplane provider wraps the same Terraform resource.
Use the Resource kind (oss.grafana.m.crossplane.io/v1alpha1) and set forProvider.manifest to the same dashboard.grafana.app/v2beta1 variable manifest as in the Terraform examples.
Next steps
- To learn more about cross-dashboard variables, refer to Cross-dashboard variables.
- To learn more about App Platform resources in Grafana Cloud, refer to Configure the Grafana provider for App Platform resources in Grafana Cloud.
- For all the options of the generic resource, refer to the
grafana_apps_generic_resourceresource documentation.


