Configure the Grafana Terraform provider

The Grafana Terraform provider connects Terraform to your Grafana instance. You declare it in your Terraform configuration and supply the URL and service account token for authentication.

To configure the provider, complete the following steps:

  1. Create a new directory for your Terraform configuration (or use your existing project directory where dashboard.json lives):

    Bash
    mkdir -p terraform && cd terraform
  2. Create a file named main.tf with the provider configuration:

    hcl
    terraform {
      required_providers {
        grafana = {
          source  = "grafana/grafana"
          version = ">= 2.9.0"
        }
      }
    }
    
    variable "grafana_url" {
      description = "Grafana instance URL"
      type        = string
    }
    
    variable "grafana_token" {
      description = "Grafana service account token"
      type        = string
      sensitive   = true
    }
    
    provider "grafana" {
      url  = var.grafana_url
      auth = var.grafana_token
    }
  3. Create a terraform.tfvars file with your credentials (or pass them as environment variables):

    hcl
    grafana_url   = "https://your-stack.grafana.net/"
    grafana_token = "glsa_your_token_here"

Warning

Add terraform.tfvars to your .gitignore to avoid committing credentials. For CI/CD, pass credentials as environment variables (TF_VAR_grafana_url and TF_VAR_grafana_token) instead.

  1. Initialize Terraform to download the Grafana provider:

    Bash
    terraform init

    You should see output indicating that the grafana/grafana provider was successfully installed.

What the provider supports

Resource typeExamples
DashboardsCreate, update, organize into folders
FoldersOrganize dashboards hierarchically
Data sourcesPrometheus, Loki, Tempo, etc.
Alert rulesAlerting configurations
Service accountsAPI credentials

The provider supports Grafana Cloud, Enterprise, and OSS instances.

In the next milestone, you add a grafana_dashboard resource that references your JSON file.


page 4 of 8