Manage integration policies by using Terraform

Updated at:

Create, import, and delete CloudMonitor integration policies by using Terraform to automate metric collection from Alibaba Cloud services such as ECS, Container Service for Kubernetes (ACK), Elasticsearch, and Kafka.

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. Use 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. Avoid using your Alibaba Cloud account directly. 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 integration policy, such as cn-hangzhou. To find available region IDs, see Regions and zones.

    • 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.

Create an integration policy

Important

The entity_group block is only required when policy_type is set to CS (Container Service for Kubernetes). For other policy types (ECS, Cloud, ES, KAFKA), omit the entity_group block. CloudMonitor automatically discovers and monitors all resources of the specified type.

Create a CS-type integration policy

When policy_type is set to CS (Container Service for Kubernetes), you must configure the entity_group block to specify the target cluster.

  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-hangzhou"
      endpoints {
        cms = "metrics.cn-hangzhou.aliyuncs.com"
      }
    }
    
    resource "alicloud_cms_integration_policy" "this" {
      workspace                = "<your-workspace-name>"
      integration_policy_name  = "<policy-name>"
      policy_type              = "CS"
    
      entity_group {
        cluster_entity_type   = "acs.ack.cluster"
        cluster_id = "your-cluster-id"
      }
    }
    
    output "integration_policy_id" {
      value = alicloud_cms_integration_policy.this.id
    }

    The following table describes the parameters for a CS-type integration policy:

    Parameter Description Required
    workspace The name of the workspace. To find the workspace name, navigate to Workspace Management in the CloudMonitor console. Yes
    integration_policy_name The name of the integration policy. Must be unique within the workspace. Yes
    policy_type The type of the policy. Set the value to CS for Container Service for Kubernetes. Changing this forces a new resource to be created. Yes
    entity_group The entity group configuration. Includes cluster_entity_type (the entity type, set to acs.ack.cluster for ACK clusters) and cluster_id (the ID of the ACK cluster to monitor). Required only when policy_type is CS. Yes (for CS type only)
  2. Initialize and apply the configuration:

    terraform init
    terraform plan
    terraform apply

    When prompted, enter yes to confirm.

Create an ECS, Cloud, ES, or KAFKA-type integration policy

When policy_type is set to ECS, Cloud, ES, or KAFKA, the entity_group block is not required. CloudMonitor automatically discovers and monitors all resources of the specified type in the 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-hangzhou"
      endpoints {
        cms = "metrics.cn-hangzhou.aliyuncs.com"
      }
    }
    
    resource "alicloud_cms_integration_policy" "this" {
      workspace                = "<your-workspace-name>"
      integration_policy_name  = "<policy-name>"
      policy_type              = "ECS"
    }
    
    output "integration_policy_id" {
      value = alicloud_cms_integration_policy.this.id
    }

    The following table describes the parameters:

    Parameter Description Required
    workspace The name of the workspace. Yes
    integration_policy_name The name of the integration policy. Must be unique within the workspace. Yes
    policy_type The type of the policy. Valid values: ECS, Cloud, ES, and KAFKA. Changing this forces a new resource to be created. Yes
    Note

    When policy_type is not CS, the entity_group block is not required and should be omitted. CloudMonitor automatically discovers and monitors all resources of the specified type in the workspace.

  2. Initialize and apply the configuration:

    terraform init
    terraform plan
    terraform apply

    When prompted, enter yes to confirm.

Import an existing integration policy

To manage an existing integration policy with Terraform, use one of the following methods.

Import block (Terraform v1.5+)

Add an import block to your configuration.

import {
  to = alicloud_cms_integration_policy.this
  id = "<integration-policy-id>"
}

CLI import

Run the following command.

terraform import alicloud_cms_integration_policy.this <integration-policy-id>

Delete an integration policy

Run the following command to delete the integration policy managed by Terraform.

terraform destroy

When prompted, enter yes to confirm.

Expected output:

alicloud_cms_integration_policy.this: Destroying... [id=xxx]
alicloud_cms_integration_policy.this: Destruction complete after 3s

Destroy complete! Resources: 1 destroyed.
Warning

This operation is irreversible. After you run terraform destroy, the integration policy is permanently deleted and CloudMonitor stops collecting metrics for the associated resources.

Timeouts

The following default timeouts apply to this resource.

Operation Default timeout
Create 5 minutes

Update

5 minutes

Delete 5 minutes

Common errors

The following table describes common errors and solutions.

Symptom Cause Resolution
Error: InvalidParameter.policy_type The specified policy_type value is not a valid option. Use one of the valid values: CS, ECS, Cloud, ES, or KAFKA.
Error: MissingParameter.entity_group The entity_group block is missing for a CS-type policy. The entity_group block is required when policy_type is CS. Add the entity_group block with a valid cluster_entity_type and the cluster_id of the ACK cluster to monitor.
Error: Forbidden.RAM The RAM user does not have the required permissions to manage integration policies. Attach the AliyunCMSFullAccess policy to the RAM user, or create a custom policy that grants cms:CreateIntegrationPolicy and cms:DeleteIntegrationPolicy permissions.
Error: DuplicatePolicyName An integration policy with the same name already exists in the workspace. Use a different policy name, or import the existing policy into Terraform state by using the terraform import command.
Error: InvalidClusterId in cluster_id The ACK cluster ID specified in cluster_id does not exist or is in a different region. Verify the cluster ID in the Container Service for Kubernetes console. Make sure the cluster is in the same region specified in the provider block.