This is documentation for the next version of Grafana documentation. For the latest stable release, go to the latest version.
Configure the Knowledge Graph stack using Terraform
The Knowledge Graph stack resource manages the full onboarding flow for your Grafana Cloud stack. It provisions API tokens, configures datasets based on available metrics, and enables the stack.
Note
The Grafana Terraform provider is updated frequently. For the latest provider version and the complete reference for every resource and data source, refer to the Grafana provider in the Terraform Registry.
By default, datasets are auto-configured based on detected metrics. To manually configure datasets, for example when your metrics use non-standard label names, use the dataset block.
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
- The Grafana Terraform provider configured, as shown in Get started with Terraform for Knowledge Graph
- A Grafana Cloud Access Policy token with the
accesspolicies:read,accesspolicies:write,accesspolicies:delete,stacks:read, andstack-service-accounts:writescopes, if you create the required stack tokens with Terraform in the next section
Dataset types
The following dataset types are available:
kubernetes: Kubernetes metrics. Requires Kubernetes Monitoring to be enabled on your stack.otel: Application Observability metrics. Requires Application Observability to be enabled on your stack.prometheus: Standard Prometheus metrics.aws: Amazon Web Services metrics.
Create the required tokens
Before you configure the Knowledge Graph stack, create the Cloud Access Policy and Grafana Service Account tokens that the resource requires.
Because grafana_cloud_access_policy and grafana_cloud_stack_service_account call the Grafana Cloud API, configure a grafana.cloud provider alias with cloud_access_policy_token alongside the grafana.asserts provider from
Get started.
Create a file named knowledge-graph-stack.tf and add the following:
provider "grafana" {
alias = "cloud"
cloud_access_policy_token = var.cloud_access_policy_token
}
# Create a Cloud Access Policy with required scopes
resource "grafana_cloud_access_policy" "knowledge_graph" {
provider = grafana.cloud
name = "knowledge-graph-stack-policy"
display_name = "Knowledge Graph Stack Policy"
region = var.stack_region
scopes = [
"stacks:read",
"metrics:read",
"metrics:write",
]
realm {
type = "stack"
identifier = var.stack_id
}
}
# Create a token from the Cloud Access Policy
resource "grafana_cloud_access_policy_token" "knowledge_graph" {
provider = grafana.cloud
name = "knowledge-graph-stack-token"
access_policy_id = grafana_cloud_access_policy.knowledge_graph.policy_id
region = grafana_cloud_access_policy.knowledge_graph.region
}
# Create a Grafana Service Account for dashboards and Grafana Managed Alerts
resource "grafana_cloud_stack_service_account" "knowledge_graph" {
provider = grafana.cloud
stack_slug = var.stack_slug
name = "knowledge-graph-managed-alerts-sa"
role = "Admin"
is_disabled = false
}
resource "grafana_cloud_stack_service_account_token" "knowledge_graph" {
provider = grafana.cloud
stack_slug = var.stack_slug
service_account_id = grafana_cloud_stack_service_account.knowledge_graph.id
name = "knowledge-graph-managed-alerts-token"
}
variable "cloud_access_policy_token" {
description = "A Grafana Cloud Access Policy token for managing access policies and stack service accounts"
type = string
sensitive = true
}
variable "stack_id" {
description = "The Grafana Cloud stack ID"
type = string
}
variable "stack_slug" {
description = "The Grafana Cloud stack slug"
type = string
}
variable "stack_region" {
description = "The region of the Grafana Cloud stack, for example prod-us-east-0"
type = string
}Set the following variables, for example in a terraform.tfvars file:
cloud_access_policy_token: Your Grafana Cloud Access Policy tokenstack_id: Your Grafana Cloud stack IDstack_slug: Your Grafana Cloud stack slugstack_region: The region of your Grafana Cloud stack, for exampleprod-us-east-0
Basic stack configuration with auto-detected datasets
The simplest configuration provisions tokens and auto-detects datasets based on available metrics. This is the recommended approach when your metrics use standard label names.
Add the following to knowledge-graph-stack.tf:
resource "grafana_asserts_stack" "main" {
provider = grafana.asserts
cloud_access_policy_token = grafana_cloud_access_policy_token.knowledge_graph.token
grafana_token = grafana_cloud_stack_service_account_token.knowledge_graph.key
}When you apply this configuration, the resource:
- Provisions API tokens for Grafana Cloud, Mimir, and the assertion detector
- Auto-detects available datasets based on your stack’s metrics
- Enables the stack with the detected datasets
Configure the dataset manually
Use manual configuration when your metrics use non-standard label names, for example a custom environment label. Each dataset block configures one dataset type.
resource "grafana_asserts_stack" "manual" {
provider = grafana.asserts
cloud_access_policy_token = grafana_cloud_access_policy_token.knowledge_graph.token
grafana_token = grafana_cloud_stack_service_account_token.knowledge_graph.key
dataset {
type = "kubernetes"
filter_group {
env_label = "deployment_environment"
site_label = "cluster"
env_label_values = ["production", "staging"]
site_label_values = ["us-east-1", "eu-west-1"]
}
}
}Note
When you specify one or more
datasetblocks, auto-detection is skipped. You must define all datasets you want to configure.
Configure multiple datasets with filters
Configure multiple dataset types with custom label mappings and metric filters:
resource "grafana_asserts_stack" "multi_dataset" {
provider = grafana.asserts
cloud_access_policy_token = grafana_cloud_access_policy_token.knowledge_graph.token
grafana_token = grafana_cloud_stack_service_account_token.knowledge_graph.key
dataset {
type = "kubernetes"
filter_group {
env_label = "deployment_environment"
site_label = "cluster"
env_label_values = ["production", "staging"]
site_label_values = ["us-east-1", "eu-west-1"]
}
}
dataset {
type = "prometheus"
filter_group {
env_label = "environment"
env_name = "prod"
filter {
name = "region"
operator = "=~"
values = ["us-.*", "eu-.*"]
}
}
}
}Resource reference
grafana_asserts_stack
Manage the Knowledge Graph stack configuration through the Grafana API. This resource handles the full onboarding flow including token provisioning, dataset configuration, and stack enablement.
Arguments
Attributes
Dataset block
Each dataset block supports the following:
Filter group block
Each filter_group block supports the following:
Filter block
Each filter block supports the following:
Example
resource "grafana_asserts_stack" "example" {
provider = grafana.asserts
cloud_access_policy_token = grafana_cloud_access_policy_token.knowledge_graph.token
grafana_token = grafana_cloud_stack_service_account_token.knowledge_graph.key
dataset {
type = "kubernetes"
filter_group {
env_label = "deployment_environment"
site_label = "cluster"
env_label_values = ["production"]
site_label_values = ["us-east-1"]
}
}
}Best practices
Consider the following best practices when configuring the Knowledge Graph stack with Terraform.
Token management
- Create a dedicated Cloud Access Policy for the Knowledge Graph stack with only the required scopes
- Use separate Service Accounts for different purposes rather than sharing a single admin token
- Rotate tokens regularly and use sensitive variable handling in Terraform to avoid exposing tokens in logs
- Store tokens in a secret management system and reference them through Terraform variables
Dataset configuration
- Start with auto-detected datasets and switch to manual configuration only when needed
- When using manual configuration, define all datasets you want to monitor
- Use specific
env_label_valuesandsite_label_valuesto limit the scope of monitoring - Test dataset configurations in a non-production stack first
Filter groups
- Use filter groups when your metrics use non-standard label names
- Match your
env_labelandsite_labelvalues to the actual label names in your Prometheus metrics - Use regular expression operators (
=~,!~) in filters for flexible value matching across regions or environments - Keep filter configurations consistent across datasets for predictable behavior
Validation
After applying the Terraform configuration, verify that:
- The stack status shows as enabled by checking the
enabledoutput attribute - The
statusattribute indicates successful onboarding - Datasets are configured correctly in the Knowledge Graph UI
- Dashboards and alerting rules are installed when
grafana_tokenis provided
You can inspect the stack status using Terraform outputs:
output "stack_enabled" {
value = grafana_asserts_stack.main.enabled
description = "Whether the Knowledge Graph stack is enabled"
}
output "stack_status" {
value = grafana_asserts_stack.main.status
description = "Current onboarding status of the stack"
}
output "stack_version" {
value = grafana_asserts_stack.main.version
description = "Configuration version number"
}

