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:
- A Grafana Cloud account, as shown in Get started
- Terraform installed on your machine
- Administrator permissions in your Grafana instance
- Knowledge Graph enabled in your Grafana Cloud stack
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
Create a Service account and token in Grafana.
To create a new one, refer to Service account tokens.
Create a file named
main.tf
and add the following: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>" }
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:
Initialize a working directory containing Terraform configuration files:
terraform init
Preview the changes that Terraform makes:
terraform plan
Apply the configuration files:
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: