Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/grafana/opentofu-grafana-example

Deploy a datasource, a folder and a dashboard in Grafana using OpenTofu (open-source Terraform fork)
https://github.com/grafana/opentofu-grafana-example

grafana opentofu

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

        




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](https://img.shields.io/badge/CREATE%20A%20CLOUD%20ACCOUNT-F46800?style=for-the-badge&logo=grafana&logoColor=white)](https://grafana.com/auth/sign-up/create-user?cta=create-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](https://opentofu.org/docs/intro/install/) 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=' -var 'my_token=' -var 'my_email='

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=' -var 'my_token='

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

---

## Acknowledgments

Some useful ressources from Grafana Labs and friends:

* [OpenTofu Manifesto](https://opentofu.org/manifesto/)
* [Grafana Grizzly: CLI for Observability as Code](https://grafana.github.io/grizzly/)
* [Web Shop O11y demo](https://github.com/Condla/web-shop-o11y-demo)
* [On demand videos and webinars](https://grafana.com/videos/)
* [Docs for LGTM, k6, OnCall, etc.](https://grafana.com/docs/)
* [Next Grafana Labs events](https://grafana.com/about/events/)

(back to top)