Manage Integration Center configurations by using Terraform

Updated at:

Create, import, and delete Integration Center configurations (AddonRelease) for Managed Service for Prometheus in CloudMonitor by using Terraform.

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

    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, 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="cn-hangzhou"

    Replace the value of ALICLOUD_REGION with the ID of the region in which you want to create the resource, such as cn-hangzhou.

    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     = "cn-hangzhou"
      endpoints {
        cms = "cms.cn-hangzhou.aliyuncs.com"
      }
    }
    Important

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

  • A CloudMonitor workspace is created. If you do not have a workspace, create one in the CloudMonitor console. For more information, see Workspace management.

  • An integration center policy (alicloud_cms_addon_policy) is created. The AddonRelease resource depends on an existing integration center policy. For more information, see Manage integration center policies by using Terraform.

Create an Integration Center configuration

An AddonRelease resource depends on an integration center policy. The following example creates an Integration Center configuration by using an existing policy ID.

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

    terraform {
      required_version = ">= 1.5.0"
    
      required_providers {
        alicloud = {
          source  = "aliyun/alicloud"
          version = "~> 1.280.0"
        }
      }
    }
    
    provider "alicloud" {
      access_key = "<your-access-key-id>"
      secret_key = "<your-access-key-secret>"
      region     = "cn-hangzhou"
      endpoints {
        cms = "cms.cn-hangzhou.aliyuncs.com"
      }
    }
    
    resource "alicloud_cms_addon_release" "default" {
      integration_policy_id = "policy-xxxxxxxx"
      addon_name            = "ahpa"
      addon_version         = "1.0.0"
      workspace             = "test-workspace"
      config                = jsonencode({
        ahpa = {
          install_ahpa_controller = false
          scrapeInterval          = 30
        }
      })
    }
    
    output "addon_release_id" {
      value = alicloud_cms_addon_release.default.id
    }

    The alicloud_cms_addon_release resource supports the following parameters.

    Parameter

    Description

    Required

    integration_policy_id

    The ID of the integration center policy that this configuration belongs to. Example: policy-xxxxxxxx. Changing this value forces a new resource to be created.

    Yes

    addon_name

    The name of the add-on to integrate. Example: ahpa. Changing this value forces a new resource to be created.

    Yes

    addon_version

    The version of the add-on. Call the ListAddons API operation to query available versions.

    Yes

    workspace

    The name of the workspace to which the add-on is installed. The workspace must match the workspace of the associated integration center policy. Changing this value forces a new resource to be created.

    No

    config

    The add-on configuration in JSON format. Use jsonencode() to convert HCL maps to JSON strings.

    No

    addon_release_name

    The name of the released add-on package. Changing this value forces a new resource to be created.

    No

    aliyun_lang

    The language of the add-on. Valid values: zh (Chinese) and en (English). Changing this value forces a new resource to be created.

    No

    env_type

    The environment type. Valid values: CS (Container Service for Kubernetes), ECS (Elastic Compute Service), and Cloud. Changing this value forces a new resource to be created.

    No

    dry_run

    Set to true to validate the configuration without creating or modifying the resource. Set to false to apply changes. Default value: false.

    No

    Note

    The integration_policy_id, addon_name, and addon_release_name parameters are ForceNew. If you change any of these values, Terraform destroys the existing resource and creates a new one.

  2. Run the following command to initialize the Terraform working directory:

    terraform init

    Expected output:

    Initializing the backend...
    
    Initializing provider plugins...
    - Finding aliyun/alicloud versions matching "~> 1.280.0"...
    - Installing aliyun/alicloud v1.277.0...
    - Installed aliyun/alicloud v1.277.0 (signed by a HashiCorp verified partner)
    ...
    
    Terraform has been successfully initialized!
  3. Run the following command to preview the execution plan:

    terraform plan

    Expected output:

    ...
    Plan: 1 to add, 0 to change, 0 to destroy.
    ...
  4. Run the following command to create the AddonRelease resource:

    terraform apply

    When prompted, enter yes to confirm.

    Expected output:

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

Import an existing resource

To manage an existing AddonRelease resource with Terraform, use one of the following methods.

Import block (Terraform v1.5+)

Add an import block to your configuration:

import {
  to = alicloud_cms_addon_release.default
  id = "<addon-release-id>"
}

CLI import

Run the following command:

terraform import alicloud_cms_addon_release.default <addon-release-id>

Delete an Integration Center configuration

  1. Run the following command to delete the AddonRelease resource created by Terraform:

    terraform destroy
  2. When prompted, enter yes to confirm.

    Expected output:

    ...
    alicloud_cms_addon_release.default: Destroying... [id=************]
    alicloud_cms_addon_release.default: Destruction complete after 5s
    
    Destroy complete! Resources: 1 destroyed.
  3. Log in to the Alibaba Cloud console.

  4. In the left-side navigation pane, choose Managed Service for Prometheus > Integration Center.

  5. Verify that the integration configuration no longer appears in the list.

Warning

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

Troubleshooting

  • Authentication failure: Verify that ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY environment variables are set correctly, or that the Provider block credentials are valid.

  • ResourceNotFound: The specified integration center policy does not exist: Verify that the integration_policy_id parameter references an existing policy. Run terraform show to inspect the current policy ID.

  • Workspace mismatch: Ensure that the workspace parameter matches the workspace of the referenced integration center policy. Both resources must belong to the same workspace.

Timeouts

Default timeouts for the alicloud_cms_addon_release resource:

Operation

Default timeout

Create

5 minutes

Update

5 minutes

Delete

5 minutes