0

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

Question

How to import a dashboard into multiple Grafana instances with Terraform?

dur
  • 15,689
  • 25
  • 79
  • 125

1 Answers1

1

1.) It looks like your test.json was exporter with Export for sharing externally enabled = 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 externally disabled - now dashboard will have hardcoded datasources uid, e.g.:

         "datasource": {
            "type": "mysql",
            "uid": "<random-hardcoded-string>"
          },

But that uid can be changed each time you provision datasource. So it is good idea to predefine uid in the datasource resource, e.g.

resource "grafana_data_source" "mysql" {
  uid  = "my-mysql"
  type = "mysql"
  name = "MySQL"
...

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 uid values in test.json by current resource.grafana_data_source.mysql.uid.

Jan Garaj
  • 25,598
  • 3
  • 38
  • 59
  • *it can be hardcoded in the test.json.* I don't want to change `test.json` manually. I would need to change it every time, I edit the dashboard on the first instance. – dur Aug 21 '23 at 19:17
  • *More advance option will be to use string function to replace all old datasource uid values in test.json by current resource.grafana_data_source.mysql.uid.* That could be a solution. Not nice, but I will try it. – dur Aug 21 '23 at 19:18