Menu
Grafana Cloud

Get started with Terraform for Knowledge Graph

Learn how to configure Terraform to manage Grafana Cloud Knowledge Graph resources. This guide walks you through setting up the Grafana Terraform provider and preparing your environment.

Before you begin

Before you begin, ensure you have the following:

Note

All Terraform configuration files should be saved in the same directory.

Configure the Grafana provider

This Terraform configuration sets up the Grafana provider to provide necessary authentication when managing Knowledge Graph resources.

You can reuse a similar setup to the one described in Creating and managing a Grafana Cloud stack using Terraform to set up a service account and a token.

Steps

  1. Create a Service account and token in Grafana.

    To create a new one, refer to Service account tokens.

  2. Create a file named main.tf and add the following:

    terraform
    terraform {
      required_providers {
        grafana = {
          source  = "grafana/grafana"
          version = ">= 2.9.0"
        }
      }
    }
    
    provider "grafana" {
      alias = "asserts"
    
      url      = "<Stack-URL>"
      auth     = "<Service-account-token>"
      stack_id = "<Stack-ID>"
    }
  3. Replace the following field values:

    • <Stack-URL> with the URL of your Grafana stack (for example, https://my-stack.grafana.net/)
    • <Service-account-token> with the service account token that you created
    • <Stack-ID> with your Grafana Cloud stack ID

Note

The stack_id parameter is required for Knowledge Graph resources to identify the stack where the resources belong.

Apply Terraform configurations

After creating your Terraform configuration files, apply them using the following commands:

  1. Initialize a working directory containing Terraform configuration files:

    shell
    terraform init
  2. Preview the changes that Terraform makes:

    shell
    terraform plan
  3. Apply the configuration files:

    shell
    terraform apply

Verify your setup

After applying the configuration, verify your setup by checking that:

  • Terraform can authenticate with your Grafana Cloud stack
  • The provider is properly configured with the correct stack ID
  • No errors appear in the Terraform output

Best practices

When managing Knowledge Graph resources with Terraform, consider the following best practices:

Name conventions

  • Use descriptive names that clearly indicate the purpose of each resource
  • Follow a consistent naming pattern across your organization
  • Include environment or team identifiers in names when appropriate

Version control

  • Store your Terraform configurations in version control (Git)
  • Use separate directories or workspaces for different environments
  • Document changes in commit messages

State management

  • Use remote state backends for team collaboration
  • Enable state locking to prevent concurrent modifications
  • Regularly back up your Terraform state files

Security

  • Never commit service account tokens or sensitive data to version control
  • Use environment variables or secret management tools for credentials
  • Rotate service account tokens regularly

Next steps

Now that you have configured the Terraform provider, you can start managing Knowledge Graph resources: