Adaptive Metrics Terraform provider
Use the Adaptive Metrics Terraform provider to configure aggregation rules, recommendations, and exemptions.
Prerequisites
Before you begin, you should have the following available:
- A Grafana Cloud account, as shown in Get started
- Terraform installed on your machine
Note
All of the following Terraform configuration files should be saved in the same directory.
Configure the Terraform provider
Create a file named
main.tf
and add the following:terraform { required_providers { grafana-adaptive-metrics = { source = "registry.terraform.io/grafana/grafana-adaptive-metrics" } } } provider "grafana-adaptive-metrics" { url = "<your-grafana-cloud-prom-url>" api_key = "<your-numeric-instance-id>:<your-cloud-access-policy-token>" }
Replace the following field values:
<your-grafana-cloud-prom-url>
with the URL of your hosted Prometheus endpoint in the formhttps://<something>.grafana.net
. To find this URL, go to yourgrafana.com
account and check the Details page of your hosted Prometheus endpoint.<your-numeric-instance-id>
with the numeric instance ID where you want to use Adaptive Metrics. To find this value, go to your grafana.com account and check the Details page of your hosted Prometheus endpoint for Username / Instance ID.
<your-cloud-access-policy-token>
with a token from a Grafana Cloud Access Policy. Make sure the access policy hasmetrics:read
andmetrics:write
scopes for the stack ID where you want to use Adaptive Metrics.
Add aggregation rules
This example Terraform configuration creates aggregation rules for test_metric_1
and test_metric_2
using grafana-adaptive-metrics_rule (Resource).
Create a file named aggregation-rules.tf
and add the following:
resource "grafana-adaptive-metrics_rule" "test_metric_1" {
metric = "test_metric_1"
drop_labels = ["pod", "instance"]
aggregations = ["sum:counter"]
}
resource "grafana-adaptive-metrics_rule" "test_metric_2" {
metric = "test_metric_2"
drop_labels = ["pod"]
aggregations = ["sum:counter", "sum", "count"]
}
Add recommendations exemptions
This example Terraform configuration creates an exemption for test_metric_1
using grafana-adaptive-metrics_exemption (Resource).
Create a file named aggregation-exemptions.tf
and add the following:
resource "grafana-adaptive-metrics_exemption" "ex1" {
metric = "test_metric_1"
keep_labels = ["namespace"]
}
Configure recommendations service
This example Terraform configuration brings the recommendations configuration singleton resource under Terraform management using grafana-adaptive-metrics_recommendations_config (Resource).
Create a file named aggregation-recommendations-config.tf
and add the following:
resource "grafana-adaptive-metrics_recommendations_config" "singleton" {
keep_labels = ["namespace"]
}
Note
The recommendations configuration is a singleton resource that always exists for every tenant. Creating it in Terraform adds it to Terraform state but the underlying resource already exists. Deleting it removes it from Terraform state but leaves the underlying resource.
Apply the Terraform configuration
In a terminal, run the following commands from the directory where all of the configuration files are located.
Initialize a working directory containing Terraform configuration files.
terraform init
Preview the changes.
terraform plan
Apply the configuration files.
terraform apply