Manage Prometheus aggregated views by using Terraform

Updated at:

Terraform lets you create, import, and delete Prometheus aggregated views that combine metrics from one or more Prometheus instances into a unified query interface.

Prerequisites

Make sure that the following requirements are met:

  • Terraform is installed.

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

    • 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

    Make sure that your Terraform version is 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="<your-access-key-id>"
      export ALICLOUD_SECRET_KEY="<your-access-key-secret>"
      export ALICLOUD_REGION="<region-id>"
      Note

      Replace <region-id> with the ID of the region in which you want to create the aggregated view, such as cn-beijing.

    • Method 2: Provider configuration

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

      provider "alicloud" {
        access_key = "<your-access-key-id>"
        secret_key = "<your-access-key-secret>"
        region     = "<region-id>"
        endpoints {
          cms = "metrics.<region-id>.aliyuncs.com"
        }
      }
      Important

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

  • At least one Prometheus instance is created. For more information, see Manage Prometheus instances by using Terraform.

Important

All Prometheus instances included in an aggregated view must belong to the same workspace. You cannot combine instances from different workspaces into a single view.

Create a Prometheus aggregated view

Create a single-instance view

To create an aggregated view for a single Prometheus instance:

  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 = "<your-access-key-id>"
      secret_key = "<your-access-key-secret>"
      region     = "cn-beijing"
      endpoints {
        cms = "metrics.cn-beijing.aliyuncs.com"
      }
    }
    
    data "alicloud_account" "default" {
    }
    
    resource "alicloud_cms_prometheus_instance" "this" {
      prometheus_instance_name = "<instance-name>"
      workspace               = "<your-workspace-name>"
    }
    
    resource "alicloud_cms_prometheus_view" "default" {
      prometheus_view_name = "<view-name>"
      version              = "V2"
      workspace            = alicloud_cms_prometheus_instance.this.workspace
    
      prometheus_instances {
        prometheus_instance_id = alicloud_cms_prometheus_instance.this.id
        region_id              = alicloud_cms_prometheus_instance.this.region_id
        user_id                = data.alicloud_account.default.id
      }
    }
    
    output "prometheus_view_id" {
      value = alicloud_cms_prometheus_view.default.id
    }

    The following table describes the key parameters for the alicloud_cms_prometheus_view resource:

    Parameter Description Required
    prometheus_view_name The name of the aggregated view. Yes
    version The version of the view. Set the value to V2. Yes
    workspace The name of the workspace. All instances in the view must belong to this workspace. Yes
    prometheus_instances The Prometheus instances to include in the view. Each block requires prometheus_instance_id, region_id, and user_id. All instances must belong to the same workspace. Yes
  2. Initialize and apply the configuration:

    terraform init
    terraform plan
    terraform apply

    When prompted, enter yes to confirm.

Create a multi-instance aggregated view

To aggregate metrics from multiple Prometheus instances into a single view, add multiple prometheus_instances blocks.

Important

All instances in a multi-instance aggregated view must belong to the same workspace. If you need to aggregate metrics across workspaces, create separate views for each workspace.

  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 = "<your-access-key-id>"
      secret_key = "<your-access-key-secret>"
      region     = "cn-beijing"
      endpoints {
        cms = "metrics.cn-beijing.aliyuncs.com"
      }
    }
    
    data "alicloud_account" "default" {
    }
    
    # Create the first Prometheus instance
    resource "alicloud_cms_prometheus_instance" "instance1" {
      prometheus_instance_name = "prometheus-instance-1"
      workspace               = "<your-workspace-name>"
    }
    
    # Create the second Prometheus instance
    resource "alicloud_cms_prometheus_instance" "instance2" {
      prometheus_instance_name = "prometheus-instance-2"
      workspace               = "<your-workspace-name>"
    }
    
    # Create an aggregated view that includes both instances
    resource "alicloud_cms_prometheus_view" "default" {
      prometheus_view_name = "prometheus-aggregation-view"
      version              = "V2"
      workspace            = alicloud_cms_prometheus_instance.instance1.workspace
    
      # Include the first instance
      prometheus_instances {
        prometheus_instance_id = alicloud_cms_prometheus_instance.instance1.id
        region_id              = alicloud_cms_prometheus_instance.instance1.region_id
        user_id                = data.alicloud_account.default.id
      }
    
      # Include the second instance
      prometheus_instances {
        prometheus_instance_id = alicloud_cms_prometheus_instance.instance2.id
        region_id              = alicloud_cms_prometheus_instance.instance2.region_id
        user_id                = data.alicloud_account.default.id
      }
    }
    
    output "prometheus_view_id" {
      value = alicloud_cms_prometheus_view.default.id
    }
    Note

    Constraints:

    • All instances must belong to the same workspace. The workspace value for the view and all referenced instances must match.

    • The aggregated view automatically combines the metrics data from all specified instances.

  2. Initialize and apply the configuration:

    terraform init
    terraform plan
    terraform apply

    When prompted, enter yes to confirm.

Import an existing aggregated view

To manage an existing aggregated view with Terraform, use one of the following methods.

Import block (Terraform v1.5+)

Add an import block to your configuration:

import {
  to = alicloud_cms_prometheus_view.default
  id = "<prometheus-view-id>"
}

CLI import

Run the following command:

terraform import alicloud_cms_prometheus_view.default <prometheus-view-id>

Delete an aggregated view

Run the following command to delete the aggregated view and all associated resources managed by Terraform:

terraform destroy

When prompted, enter yes to confirm.

Expected output:

alicloud_cms_prometheus_view.default: Destroying... [id=xxx]
alicloud_cms_prometheus_view.default: Destruction complete after 3s

Destroy complete! Resources: 1 destroyed.
Note

terraform destroy deletes all resources defined in the current configuration file. To delete only the view and retain the instances, first remove the view Resource definition from the configuration file, and then run terraform apply.

Timeouts

The following default timeouts apply to this resource:

Operation Default timeout
Create 5 minutes

Update

5 minutes

Delete 5 minutes

Common errors

Common errors and solutions:

Symptom Cause Resolution
Error: InvalidParameter.workspace or workspace mismatch error The instances specified in the prometheus_instances blocks do not belong to the same workspace as the view, or the workspace name is incorrect. Make sure all instances and the view reference the same workspace. Verify the workspace name in the CloudMonitor console under Workspace Management.
Error: Forbidden.RAM The RAM user does not have the required permissions to manage Prometheus views. Attach the AliyunCMSFullAccess policy to the RAM user, or create a custom policy that grants cms:CreatePrometheusView and cms:DeletePrometheusView permissions.
Error: InvalidInstanceId The prometheus_instance_id does not exist or the instance has been deleted. Verify that the referenced Prometheus instances exist by checking the Instance List in the Managed Service for Prometheus console. If the instance was deleted, remove the corresponding prometheus_instances block.
Terraform plan shows unexpected changes after manual console edits The aggregated view was modified through the console, causing drift between the Terraform state and the actual resource. Run terraform plan to review the drift. Use terraform apply to reconcile the state, or update your main.tf to match the desired configuration.