Enterprise Open source Grafana Cloud

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:

  1. Create a service account.

  2. 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.

  3. 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
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:

sh
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 example http://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:

terraform
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 example http://prometheus:9090.

Add a folder

Create a file named folder.tf and add the following grafana_folder resource:

terraform
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:

terraform
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.

  1. Initialize the working directory and install the Grafana provider:

    sh
    terraform init
  2. Preview the changes that Terraform makes:

    sh
    terraform plan
  3. Apply the configuration:

    sh
    terraform 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: