I have multiple instances of Grafana. All instances should have the same dashboard. Therefore, I created the dashboard on one instance and exported it (see Export a dashboard). Now, I want to import the dashboard into another Grafana instance using the Grafana Provider.
I could create the dashboard on the other instance, but I couldn't open it. I get following error:
Templating
Failed to upgrade legacy queries Datasource ${DS_MYSQL} was not found
Terraform
resource "grafana_data_source" "mysql" {
type = "mysql"
name = "MySQL"
url = "localhost:3306"
database_name = "test"
username = "dbuser"
secure_json_data_encoded = jsonencode({
password = "dbpassword"
})
}
resource "grafana_dashboard" "metrics" {
config_json = file("test.json")
}
Dashboard
{
"__inputs": [
{
"name": "DS_MYSQL",
"label": "MySQL",
"description": "",
"type": "datasource",
"pluginId": "mysql",
"pluginName": "MySQL"
}
],
"__elements": {},
"__requires": [
{
"type": "grafana",
"id": "grafana",
"name": "Grafana",
"version": "10.0.3"
},
{
"type": "datasource",
"id": "mysql",
"name": "MySQL",
"version": "1.0.0"
},
{
"type": "panel",
"id": "table",
"name": "Table",
"version": ""
}
],
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- 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,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": null,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "mysql",
"uid": "${DS_MYSQL}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"custom": {
"align": "auto",
"cellOptions": {
"type": "auto"
},
"inspect": false
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 0
},
"id": 1,
"options": {
"cellHeight": "sm",
"footer": {
"countRows": false,
"fields": "",
"reducer": [
"sum"
],
"show": false
},
"showHeader": true
},
"pluginVersion": "10.0.3",
"targets": [
{
"dataset": "test",
"datasource": {
"type": "mysql",
"uid": "${DS_MYSQL}"
},
"editorMode": "builder",
"format": "table",
"rawSql": "SELECT id FROM test.issue LIMIT 50 ",
"refId": "A",
"sql": {
"columns": [
{
"parameters": [
{
"name": "id",
"type": "functionParameter"
}
],
"type": "function"
}
],
"groupBy": [],
"limit": 50
},
"table": "issue"
}
],
"title": "Panel Title",
"type": "table"
}
],
"refresh": "",
"schemaVersion": 38,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "DUR Test",
"uid": "ff8f4277-eb0b-46ee-abbf-3b668e9bd3a5",
"version": 2,
"weekStart": ""
}
Research
I read Managing Grafana Dashboards With Terraform, but there is nothing written about data sources.
I read Creating and managing a Grafana Cloud stack using Terraform, but there is no example of dashboard JSON source code.
I read [Bug] Cannot import data sources from __input field, but there is no automated solution. I don't want to change all dashboards in all instance manually.
Question
How to import a dashboard into multiple Grafana instances with Terraform?
1.) It looks like your
test.jsonwas exporter withExport for sharing externallyenabled = it has input variable, where user will select in the UI which datasource will be used. But you are not working with UI, so don't export it with this option.2.) You will export dashboard with
Export for sharing externallydisabled - now dashboard will have hardcoded datasources uid, e.g.:But that
uidcan be changed each time you provision datasource. So it is good idea to predefine uid in the datasource resource, e.g.Now you can destroy and recreate and datasource will have still the same uid - it can be hardcoded in the
test.json.More advance option will be to use string function to replace all old datasource
uidvalues intest.jsonby currentresource.grafana_data_source.mysql.uid.