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:

    Bash
    mkdir -p terraform-alerts && cd terraform-alerts
  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.

In the next milestone, you define a folder and an alert rule group.


page 4 of 10