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 Free account. No credit card required.
You can set OpenTofu the way you want. If you never did that, here are the steps you can follow:
- Install OpenTofu using the official documentation for your environment.
- Initialize OpenTofu by running
tofu init
- Go to the Service Account page: go to your Grafana instance, open the menu and click on Administration > Services Account
- Create a new Service Account with permissions on folders, datasources, alerts and dashboards
- 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.
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 createdmy_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.
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.
Some useful ressources from Grafana Labs and friends: