Using Terraform with Kloudfuse

If you use terraform and choose to manage your dashboards and alerts (custom as well as ones you install using catalog service) using terraform, please follow these steps (requires terraform grafana plugin)

Note: For specific questions or scenarios not covered here, feel free to contact the Kloudfuse team.

Terraform provider definition

Create a terraform resource file kfuse.tf and add a provider resource with terraform using following info (make sure to change url and auth as required):

terraform { required_providers { grafana = { source = "grafana/grafana" version = ">= 1.40.1" } } } provider "grafana" { url = "https://<your-kloudfuse-access-url>/grafana" auth = "admin:password" # use following format if using api key #auth = "glsa_n1BPVVcnBiDvhu3PmgrvpRJF47vl8MfO_cbb375b2" }

Add Dashboard Resources to Terraform

Once initialized the dashboards and alerts have to be imported into this terraform instance. Importing dashboards is rather easy and can be done by simply adding following resource in your kfuse.tf file. The example below shows how to import all dashboards within a custom folder custom_db_folder (as described in the documentation here)

# Create folder custom_db_folder # resource "grafana_folder" "custom_db_folder" { provider = grafana title = "my custom folder" } # Add all dashboards resource "grafana_dashboard" "custom-dbs" { provider = grafana for_each = fileset("${path.module}/<path-to-custom-db-folder>", "*.json") config_json = file("${path.module}/<path-to-custom-db-folder>/${each.key}") folder = grafana_folder.custom_db_folder.id # uncomment to force apply. # overwrite = true }

Note: In case of failures to apply the state due to conflict in the dashboard (this can happen if the dashboard already exists or there’s a UID clash. Force the import using overwrite flag.

Add Alerting Resources to Terraform

Configuring alerting related resources into terraform has following steps. Each related resource has to be explicitly configured. Currently, there’s no file base load support (unlike dashboards)

Note: The following instructions will work if there are no conflicting resources. If you have existing/conflicting resources in grafana instance, then you must import those individually first prior to updating the resource in terraform config. Please follow the import instructions for each of them as applicable.

Add Contact Points & Templates to Terraform

For each contact point which needs to be managed and optionally associated message template as a terraform resource execute following steps.

If conflicting resources exist

  1. Update kfuse.tf to include the contact point name which is conflicting (or needs importing). Using tf-test-slack-contact-point as example contact point

resource "grafana_contact_point" "tf-test-slack-contact-point" { # (resource arguments) name = "tf-test-slack-contact-point" }
  1. Run terraform import grafana_contact_point.grafana_contact_point tf-test-slack-contact-point

  2. Running the above command will update the terraform.tfstate file. Use the necessary arguments for the contact points from the file and update the kfuse.tffile. Update each required argument (plugin provider has the information on the required parameter per contact point type). For example tf-test-slack-contact-point update the kfuse.tf file as follows:

  1. Run terraform plan -auto-approve

Add Notification Policy to Terraform

Notification policies are maintained as a single object. Follow these steps to configure terraform to manage notification policies.

If conflicting resources exist

  1. Update kfuse.tf to include the a notification policy name for example current-notifciation-policy

  1. Run terraform import grafana_notification_policy.current-notifciation-policy current-notifciation-policy

  2. Running the above command will update the terraform.tfstate file. Use the necessary arguments for the contact points from the file and update the kfuse.tffile. Update each required argument (plugin provider has the information on the required parameter for notification policy). For example:

  1. Run terraform plan -auto-approve

Add Datasource config to Terraform

Kfuse datasources are already created. Import them, they will be used when defining alert rules. Update kfuse.tf as following:

Add Alert Rules to Terraform

For managing alert rules with terraform, follow these steps for each rule group. Make sure to use the grafana_data_source.KfuseDatasource.uid and kfuse-tf-test-folder in the alert rules. Here’s a working example of creating a test rulegroup with 1 rule in it. When an alert fires a notification is sent to the tf-test-slack-contact-point because it is associated with the notification policy which matches the alert labels: