---
title: "Adaptive Metrics Terraform provider | Grafana Cloud documentation"
description: "Use the Adaptive Metrics Terraform provider to configure rules, recommendations, and exemptions."
---

# 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](../../../../get-started/)
- [Terraform](https://developer.hashicorp.com/terraform/install) installed on your machine

> Note
> 
> All of the following Terraform configuration files should be saved in the same directory.

## Configure the Terraform provider

1. Create a file named `main.tf` and add the following:
   
   terraform ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```terraform
   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>"
   }
   ```
2. Replace the following field values:

<!--THE END-->

- `<your-grafana-cloud-prom-url>` with the URL of your hosted Prometheus endpoint in the form `https://<something>.grafana.net`. To find this URL, go to your [`grafana.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**.

<!--THE END-->

- `<your-cloud-access-policy-token>` with a token from a [Grafana Cloud Access Policy](../../../../security-and-account-management/authentication-and-permissions/access-policies/). Make sure the access policy has `metrics:read` and `metrics: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)](https://registry.terraform.io/providers/grafana/grafana-adaptive-metrics/latest/docs/resources/rule).

Create a file named `aggregation-rules.tf` and add the following:

terraform ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```terraform
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)](https://registry.terraform.io/providers/grafana/grafana-adaptive-metrics/latest/docs/resources/exemption).

Create a file named `aggregation-exemptions.tf` and add the following:

terraform ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```terraform
resource "grafana-adaptive-metrics_exemption" "ex1" {
    metric       = "test_metric_1"
    keep_labels  = ["namespace"]
}
```

You can also use the `match_type` attribute to exempt metrics by prefix or suffix. If not specified, the default is `exact`.

terraform ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```terraform
resource "grafana-adaptive-metrics_exemption" "ex2" {
    metric      = "http_requests_"
    match_type  = "prefix"
    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)](https://registry.terraform.io/providers/grafana/grafana-adaptive-metrics/latest/docs/resources/recommendations_config).

Create a file named `aggregation-recommendations-config.tf` and add the following:

terraform ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```terraform
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.

1. Initialize a working directory containing Terraform configuration files.
   
   shell ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```shell
   terraform init
   ```
2. Preview the changes.
   
   shell ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```shell
   terraform plan
   ```
3. Apply the configuration files.
   
   shell ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```shell
   terraform apply
   ```
