Manage Prometheus aggregation tasks by using Terraform

Updated at:

Use Terraform to create, import, and delete Prometheus aggregation tasks (alicloud_cms_agg_task_group). Aggregation tasks define rules for combining and processing metrics across Prometheus instances.

Prerequisites

Before you begin, meet the following requirements:

  • Terraform is installed on your machine.

    • Cloud Shell has Terraform and Alibaba Cloud account credentials preconfigured. No additional setup is needed.

    • If you do not use Cloud Shell, install and configure Terraform on your local machine. For more information, see Install and configure Terraform locally.

    Note

    Your Terraform version must be 1.5.0 or later. Run terraform --version to check the version.

  • Alibaba Cloud credentials are configured by using one of the following methods:

    Note

    For security best practices, we recommend that you create a RAM user named Terraform and grant it the required permissions instead of using your Alibaba Cloud account. For more information, see Create a RAM user and Grant permissions to a RAM user.

    • Method 1: Environment variables

      Create environment variables to store your credentials:

      export ALICLOUD_ACCESS_KEY="************"
      export ALICLOUD_SECRET_KEY="************"
      export ALICLOUD_REGION="cn-beijing"
      Note

      Replace the value of ALICLOUD_REGION with the ID of the region in which you want to manage aggregation tasks, such as cn-beijing.

    • Method 2: Provider configuration

      Specify credentials in the provider block of your Terraform configuration file:

      provider "alicloud" {
        access_key = "************"
        secret_key = "************"
        region     = "cn-beijing"
        endpoints {
          cms = "cms.cn-beijing.aliyuncs.com"
        }
      }
      Important

      You must explicitly specify the CMS endpoint in the format cms.<region-id>.aliyuncs.com. Replace <region-id> with the actual region ID.

  • You have a Cloud Monitor workspace. If you do not have one, create a workspace in the Cloud Monitor console or by using the alicloud_cms_prometheus_instance Terraform resource.

  • You have at least two Prometheus instances: one as the data source and one as the data target. If you do not have the required instances, create them by using Terraform or the Managed Service for Prometheus console.

Create an aggregation task

Create a basic aggregation task

An aggregation task (alicloud_cms_agg_task_group) reads metrics from a source Prometheus instance and writes aggregated results to a target instance. The following example assumes that both instances already exist.

  1. Create a working directory and create a file named main.tf with the following content:

    terraform {
      required_version = ">= 1.5.0"
    
      required_providers {
        alicloud = {
          source  = "aliyun/alicloud"
          version = "~> 1.277.0"
        }
      }
    }
    
    provider "alicloud" {
      access_key = "************"
      secret_key = "************"
      region     = "cn-beijing"
      endpoints {
        cms = "cms.cn-beijing.aliyuncs.com"
      }
    }
    
    # Create an aggregation task
    resource "alicloud_cms_agg_task_group" "default" {
      source_prometheus_id  = "rw-xxxxxxxx-source"
      target_prometheus_id  = "rw-xxxxxxxx-target"
      agg_task_group_name   = "terraform-aggtaskgroup-example"
      agg_task_group_config = <<EOF
    groups:
    - name: "node.rules"
      interval: "60s"
      rules:
      - record: "node_namespace_pod:kube_pod_info:"
        expr: "max(label_replace(kube_pod_info{job=\"kubernetes-pods-kube-state-metrics\"}, \"pod\", \"$1\", \"pod\", \"(.*)\")) by (node, namespace, pod, cluster)"
    EOF
    }
    
    output "agg_task_group_id" {
      value = alicloud_cms_agg_task_group.default.id
    }

    The alicloud_cms_agg_task_group resource supports the following parameters.

    Parameter

    Description

    Required

    source_prometheus_id

    The ID of the source Prometheus instance from which the aggregation task reads metrics. Modifying this field destroys and recreates the resource.

    Yes

    target_prometheus_id

    The ID of the target Prometheus instance to which the aggregation task writes results. Modifying this field destroys and recreates the resource.

    Yes

    agg_task_group_name

    The name of the aggregation task.

    Yes

    agg_task_group_config

    The aggregation task configuration in YAML format. Defines the aggregation rules and groups. Each group contains a name, an interval, and a list of recording rules.

    Yes

    description

    The description of the aggregation task.

    No

    status

    The status of the aggregation task. Valid values: Running and Stopped.

    No

    schedule_mode

    The scheduling mode. Valid values: Cron and FixedRate. Modifying this field destroys and recreates the resource.

    No

    cron_expr

    The cron expression for scheduling. Required when schedule_mode is set to Cron.

    No

    schedule_time_expr

    The schedule time expression.

    No

    delay

    The fixed delay for scheduling, in seconds.

    No

    max_retries

    The maximum number of retries for the aggregation task.

    No

    max_run_time_in_seconds

    The maximum run time for the aggregation task, in seconds.

    No

    to_time

    The end timestamp for scheduling, in UNIX timestamp format.

    No

    precheck_string

    The precheck configuration string in JSON format.

    No

    override_if_exists

    Specifies whether to overwrite an existing resource with the same name. Valid values: true and false.

    No

    agg_task_group_config_type

    The configuration type of the aggregation task.

    No

  2. Initialize the Terraform working directory:

    terraform init

    Expected output:

    Initializing the backend...
    
    Initializing provider plugins...
    - Finding aliyun/alicloud versions matching "~> 1.277.0"...
    - Installing aliyun/alicloud v1.277.0...
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure.
  3. Preview the execution plan:

    terraform plan

    Expected output:

    Terraform will perform the following actions:
    
      # alicloud_cms_agg_task_group.default will be created
      ...
    
    Plan: 1 to add, 0 to change, 0 to destroy.
  4. Apply the configuration to create the aggregation task:

    terraform apply

    When prompted, enter yes to confirm. Expected output:

    alicloud_cms_agg_task_group.default: Creating...
    alicloud_cms_agg_task_group.default: Creation complete after 3s [id=************]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    agg_task_group_id = "************"

Create an aggregation task with scheduling

To schedule an aggregation task at a fixed rate, include scheduling parameters in the resource configuration. The following example uses FixedRate mode:

resource "alicloud_cms_agg_task_group" "schedule_example" {
  source_prometheus_id    = "rw-xxxxxxxx-source"
  target_prometheus_id    = "rw-xxxxxxxx-target"
  agg_task_group_name     = "terraform-aggtaskgroup-schedule-example"
  description             = "Aggregation task with fixed-rate scheduling"
  status                  = "Running"
  schedule_mode           = "FixedRate"
  delay                   = 60
  max_retries             = 3
  max_run_time_in_seconds = 3600
  agg_task_group_config   = <<EOF
groups:
- name: "node.rules"
  interval: "60s"
  rules:
  - record: "node_namespace_pod:kube_pod_info:"
    expr: "max(label_replace(kube_pod_info{job=\"kubernetes-pods-kube-state-metrics\"}, \"pod\", \"$1\", \"pod\", \"(.*)\")) by (node, namespace, pod, cluster)"
EOF
}

The following table describes the scheduling parameters in this example.

Parameter

Description

Required

schedule_mode

The scheduling mode. Set to FixedRate for fixed-rate scheduling. Modifying this parameter destroys and recreates the resource.

No

delay

The fixed delay for scheduling, in seconds.

No

max_retries

The maximum number of retries for the aggregation task.

No

max_run_time_in_seconds

The maximum run time for the aggregation task, in seconds.

No

Run terraform init, terraform plan, and terraform apply to create the resource. When prompted, enter yes to confirm.

Import existing resources

To manage an existing aggregation task with Terraform, use one of the following methods.

Import block (Terraform v1.5+)

Add an import block to your configuration:

import {
  to = alicloud_cms_agg_task_group.default
  id = "<agg-task-group-id>"
}

CLI import

Run the following command:

terraform import alicloud_cms_agg_task_group.default <agg-task-group-id>

Delete an aggregation task

To delete the aggregation task and all associated resources that Terraform manages, run the following command:

terraform destroy

When prompted, enter yes to confirm.

Expected output:

alicloud_cms_agg_task_group.default: Destroying... [id=************]
alicloud_cms_agg_task_group.default: Destruction complete after 3s

Destroy complete! Resources: 1 destroyed.
Warning

This operation is irreversible. After you run terraform destroy, the aggregation task is permanently deleted. To verify the deletion, check the aggregation task list in the Managed Service for Prometheus console.

Timeouts

The alicloud_cms_agg_task_group resource has the following default timeouts:

Operation

Default timeout

Create

5 minutes

Update

5 minutes

Delete

5 minutes