Skip to content

Deploy a datasource, a folder and a dashboard in Grafana using OpenTofu (open-source Terraform fork)

Notifications You must be signed in to change notification settings

grafana/opentofu-grafana-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation


OpenTofu (open-source Terraform fork) + Grafana: Example tutorial

Static Badge Static Badge

Learn by doing. Create a datasource, a folder and import dahsboards in it with panels using this datasource. You can copy this and customize it to make much more using the Grafana provider.

Create a Grafana Cloud stack

Create a Grafana Cloud Free account. No credit card required.

Create a free account


Set your OpenTofu

You can set OpenTofu the way you want. If you never did that, here are the steps you can follow:

  1. Install OpenTofu using the official documentation for your environment.
  2. Initialize OpenTofu by running tofu init

Create a Service Account token for your Grafana

  1. Go to the Service Account page: go to your Grafana instance, open the menu and click on Administration > Services Account
  2. Create a new Service Account with permissions on folders, datasources, alerts and dashboards
  3. Create a new token for this Service Account. As best practice, use an expiration date. Copy the token, it is not possible to display it later.

Deploy to your Grafana

We will deploy:

  • A folder
  • A datasource (we use Test Data, ensure it's installed on your Grafana)
  • A dashboard
  • An alert

To do that, use the following command:

tofu apply -var 'grafana_url=<CHANGE ME>' -var 'my_token=<CHANGE ME>' -var 'my_email=<CHANGE ME>'

OpenTofu will ask you to confirm. Type yes.

Replace the values for grafana_url and my_token with the right values for your environment:

  • grafana_url should look like https://mystack.grafana.net/ in Grafana Cloud (or other domains if you use OSS or Enterprise)
  • my_token is the API token you created
  • my_email is the email address to receive alerts (don't worry, the alert rule never fires in this example)

You can now visit your instance and check that the folder, dashboard, and alerts are present.

You may found no data in your dashboard when opening it. You need to reload the page as your browser cache prevents the webapp to use the new datasource.

Cleanup your environment

As Code is great because cleaning up your tests are also very fast !

With the same values, run:

tofu destroy -var 'grafana_url=<CHANGE ME>' -var 'my_token=<CHANGE ME>'

To fully cleanup the environment, you can also remove the Service Account you created manually.


Acknowledgments

Some useful ressources from Grafana Labs and friends:

(back to top)

About

Deploy a datasource, a folder and a dashboard in Grafana using OpenTofu (open-source Terraform fork)

Topics

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages