Grafana Grafana

Operate Grafana as Code: The Big Picture

What can live in files, what needs an API, and how the pieces fit together.

8 min Beginner

Click-driven Grafana works until the first time you need a second copy, a rollback, or an answer to “who changed this panel?”. This guide maps out how to run Grafana from files instead, so you know which mechanism to use for which resource before you write any YAML.

Why the UI becomes a problem

When dashboards, data sources, and alerts are created by hand, three things go wrong over time:

  • Nothing is reproducible. The state lives in Grafana’s database. Rebuilding an instance means redoing every click.
  • Changes are invisible. There’s no review step and no history beyond what Grafana’s own version log keeps.
  • Environments drift. Staging and production slowly diverge, and nobody can say how.

The fix is to treat the database as a cache and the repository as the truth. Everything Grafana needs is either provisioned from files at startup or applied by a tool that reads files.

The layers

LayerFormatWhat it controlsApplied by
Server configgrafana.ini and GF_* env varsPorts, database, auth, SMTP, logging, feature togglesGrafana at startup
Data source provisioningYAMLData sources and their credentialsGrafana at startup
Dashboard provisioningYAML provider + JSON filesDashboards and foldersGrafana, re-scanned on an interval
Alerting provisioningYAMLAlert rules, contact points, policies, mute timings, templatesGrafana at startup
Plugin provisioningYAMLSettings for already-installed plugin appsGrafana at startup
TerraformHCLAnything with an API: folders, data sources, dashboards, alerts, teams, service accountsterraform apply
Grafana OperatorKubernetes custom resourcesInstances, folders, dashboards, data sources, alert rulesA controller in the cluster
Git SyncA Repository resourceDashboards kept in a Git repository, synced both waysGrafana itself

Which one should you use?

  • One Grafana in a container or VM, a small team: server config plus file provisioning. It has no extra moving parts.
  • Kubernetes with GitOps (Flux or Argo CD): the Grafana Helm chart for the instance, with provisioning files as ConfigMaps, or the Grafana Operator if you want each dashboard and data source as its own Kubernetes object.
  • Many instances, or Grafana Cloud: Terraform, because it can also create stacks, service accounts, and teams.
  • Dashboards authored by many people: Git Sync, or a code library such as the Grafana Foundation SDK that generates the JSON.

These mix well. A common setup is file provisioning for data sources and alerting, plus Git Sync or Terraform for dashboards.

What files can’t do

Some resources have no provisioning file format. Plan for them:

  • Users, teams, organizations, and service accounts are created through the API or Terraform (or an identity provider with SSO and role mapping).
  • Folder and dashboard permissions are managed through the API, Terraform, or the Operator.
  • The first admin password is set only on first run, so changing it later takes a CLI or API call.

The guides on authentication and Terraform cover the workarounds.

Provisioned means locked

Provisioned resources are meant to be owned by files. Data sources can be marked editable: false, dashboards from a provider can reject UI saves, and provisioned alert resources can’t be edited in the UI at all. That’s the point: if someone could change them by hand, Git would stop being the truth. Remember that the UI is still useful for exploring data and drafting a panel, and the authoring workflow in the dashboards guide shows how to bring those drafts back into Git.

A mental model for a change

  1. Edit a file in the repository (a dashboard JSON, a data source YAML, an alert rule).
  2. Open a pull request, review the diff, merge.
  3. The delivery mechanism applies it: a container restart, a dashboard re-scan, a Flux reconcile, or terraform apply.
  4. Grafana’s own database is updated to match. If something goes wrong, revert the commit.

Where to go next

Run a local Grafana from a compose file (next guide), learn the configuration rules, then add data sources, dashboards, and alerts one at a time. For reference, see the Grafana documentation.

Popular Guides