[Infrastructure as Code (IaC)] Manage Grafana with Terraform Grafana Provider

Terraform Grafana Provider

Examples about using Terraform Grafana Provider to manage Grafana resources.

Prerequsites

Your must have a Grafana admin account and password, and Terraform CLI have been installed.

Grafana Admin Account and Password

Account and password provide by Grafana.

Terrraform and Terrafrom Grafana Provider

Configuration

Grafana Provider Variables

First, Specify values for Grafana Provider variables within terraform.tfvars.

1
2
3
url = "<Your Grafana URL>"

auth = "Your Grafan auth"

Grafana Provider

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Docs overview | grafana/grafana | Terraform Registry
// https://registry.terraform.io/providers/grafana/grafana/latest/docs
terraform {
required_providers {
grafana = {
source = "grafana/grafana"
version = "1.14.0"
}
}
}

# Docs overview | grafana/grafana | Terraform Registry
# https://registry.terraform.io/providers/grafana/grafana/latest/docs
provider "grafana" {
url = vars.url
# auth = vars.auth
auth = vars.auth
}

Run

Terraform Init

1
$ terraform init

It will download the Terraform Grafana Provider binary.

Terraform apply

1
terraform apply

It will dispaly the execute plan and wait for your make yes or no.
Then yes to create Grafana roles(users) and databases.

Terraform Resource

grafana_organization

Import the default grafana_organization resource MainOrg.

1
$ terraform import grafana_organization.MainOrg 1

Then, create or edit grafana_organization.tf file.

1
2
3
4
5
6
7
8
9
10
11
12
# grafana_organization.tf

# # grafana_organization | Resources | grafana/grafana | Terraform Registry
# # https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/organization
# terraform import grafana_organization.MainOrg 1
resource "grafana_organization" "MainOrg" {
create_users = false
name = "Main Org."
admins = [
"admin@localhost",
]
}

grafana_user

1
2
3
4
5
6
7
8
9
10
# grafana_user.tf

# grafana_user | Resources | grafana/grafana | Terraform Registry
# https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/user
resource "grafana_user" "cloudolife-example" {
email = "[email protected]"
name = "cloudolife Example"
login = "cloudolife-example"
password = "<Your Password>"
}

grafana_team

1
2
3
4
5
6
7
8
9
10
11
# grafana_team.tf

# grafana_team | Resources | grafana/grafana | Terraform Registry
# https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/team
resource "grafana_team" "cloudolife-example" {
name = "Cloudolife Example"
email = "[email protected]"
members = [
"[email protected]",
]
}

grafana_folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# grafana_folder.tf

# grafana_folder | Resources | grafana/grafana | Terraform Registry
# https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/folder
resource "grafana_folder" "cloudolife-example" {
title = "cloudolife-example"
}

# grafana_folder_permission | Resources | grafana/grafana | Terraform Registry
# https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/folder_permission
resource "grafana_folder_permission" "cloudolife-example" {
folder_uid = grafana_folder.cloudolife-example.uid
permissions {
team_id = grafana_team.cloudolife-example.id
permission = "Edit"
}
}

grafana_dashboard

1
2
3
4
5
6
7
8
# grafana_dashboard.tf

# grafana_dashboard | Resources | grafana/grafana | Terraform Registry
# https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/dashboard
resource "grafana_dashboard" "cloudolife-example" {
config_json = file("./json/grafana-dashboard.cloudolife-example.json")
folder = grafana_folder.cloudolife-example.id
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"type": "dashboard"
}
]
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 3,
"links": [],
"panels": [
{
"datasource": "Loki",
"gridPos": {
"h": 9,
"w": 12,
"x": 0,
"y": 0
},
"id": 1,
"options": {
"dedupStrategy": "none",
"enableLogDetails": true,
"prettifyLogMessage": false,
"showCommonLabels": false,
"showLabels": true,
"showTime": true,
"sortOrder": "Descending",
"wrapLogMessage": true
},
"targets": [
{
"expr": "{app=\"cloudolife-example\"}",
"refId": "A"
}
],
"timeFrom": null,
"timeShift": null,
"title": "cloudolife-example",
"type": "logs"
}
],
"schemaVersion": 30,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
]
},
"timezone": "",
"title": "cloudolife-example",
"uid": "sdafo2s",
"version": 8
}

Terraform destroy

1
$ terraform destroy

All resource objects will be destroyed.

FAQs

The dashboard has been changed by someone else

1
2
3
4
5
6
│ Error: status: 412, body: {"message":"The dashboard has been changed by someone else","status":"version-mismatch"}

│ with grafana_dashboard.cloudolife-example,
│ on grafana_dashboard.tf line 27, in resource "grafana_dashboard" "cloudolife-example":
│ 27: resource "grafana_dashboard" "cloudolife-example" {

Try to remove uid field in the dashbord JSON file.

1
- "uid": "dad2fs1",

Reference

[1] Docs overview | grafana/grafana | Terraform Registry - https://registry.terraform.io/providers/grafana/grafana/latest/docs

[2] grafana/terraform-provider-grafana: Terraform Grafana provider - https://github.com/grafana/terraform-provider-grafana

[3] Terraform by HashiCorp - https://www.terraform.io/

[4] Grafana: The open observability platform | Grafana Labs - https://grafana.com/