---
title: "Manage a self-managed Grafana instance using Terraform | Grafana documentation"
description: "Learn how to configure the Grafana Terraform provider for a self-managed Grafana instance and add a data source, a folder, and a dashboard to it."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/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](https://registry.terraform.io/providers/grafana/grafana/latest/docs).

## 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](https://developer.hashicorp.com/terraform/install) 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](https://grafana.com/docs/grafana/next/visualizations/dashboards/share-dashboards-panels/#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](https://grafana.com/docs/grafana/next/administration/service-accounts/#create-a-service-account-in-grafana).
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](https://grafana.com/docs/grafana/next/administration/roles-and-permissions/).
3. [Add a token to the service account](https://grafana.com/docs/grafana/next/administration/service-accounts/#add-a-token-to-a-service-account-in-grafana), and save the token for the next step.

## Configure the provider

Create a file named `provider.tf` and add the following:

terraform ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```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 ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```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](https://registry.terraform.io/providers/grafana/grafana/latest/docs#authentication).

## Add a data source

This guide uses the Prometheus data source. The required arguments for [`grafana_data_source`](https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/data_source) vary depending on the type of data source.

Create a file named `datasource.tf` and add the following:

terraform ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```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`](https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/folder) resource:

terraform ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```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`](https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/dashboard) resource:

terraform ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```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 ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```sh
   terraform init
   ```
2. Preview the changes that Terraform makes:
   
   sh ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```sh
   terraform plan
   ```
3. Apply the configuration:
   
   sh ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```sh
   terraform apply
   ```

## Verify the resources

After Terraform applies the configuration, check the following in Grafana:

- The data source appears in **Connections** &gt; **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](https://grafana.com/docs/grafana/next/as-code/terraform/grafana-resources/dashboards-github-action/).
- Provision alerting resources. Refer to [Use Terraform to provision alerting resources](https://grafana.com/docs/grafana/next/alerting/set-up/provision-alerting-resources/terraform-provisioning/).
- Explore all available resources in the [Grafana provider documentation](https://registry.terraform.io/providers/grafana/grafana/latest/docs).
