Manage Integration Center configurations by using Terraform
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.
NoteYour Terraform version must be 1.5.0 or later. Run
terraform --versionto check the version. -
Alibaba Cloud credentials are configured by using one of the following methods:
NoteFor 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_REGIONwith the ID of the region in which you want to create the resource, such ascn-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" } }ImportantYou 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.
-
Create a working directory and a file named
main.tfwith 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_releaseresource supports the following parameters.Parameter
Description
Required
integration_policy_idThe 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_nameThe name of the add-on to integrate. Example:
ahpa. Changing this value forces a new resource to be created.Yes
addon_versionThe version of the add-on. Call the ListAddons API operation to query available versions.
Yes
workspaceThe 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
configThe add-on configuration in JSON format. Use
jsonencode()to convert HCL maps to JSON strings.No
addon_release_nameThe name of the released add-on package. Changing this value forces a new resource to be created.
No
aliyun_langThe language of the add-on. Valid values:
zh(Chinese) anden(English). Changing this value forces a new resource to be created.No
env_typeThe environment type. Valid values:
CS(Container Service for Kubernetes),ECS(Elastic Compute Service), andCloud. Changing this value forces a new resource to be created.No
dry_runSet to
trueto validate the configuration without creating or modifying the resource. Set tofalseto apply changes. Default value:false.No
NoteThe
integration_policy_id,addon_name, andaddon_release_nameparameters are ForceNew. If you change any of these values, Terraform destroys the existing resource and creates a new one. -
Run the following command to initialize the Terraform working directory:
terraform initExpected 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! -
Run the following command to preview the execution plan:
terraform planExpected output:
... Plan: 1 to add, 0 to change, 0 to destroy. ... -
Run the following command to create the AddonRelease resource:
terraform applyWhen prompted, enter
yesto 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
-
Run the following command to delete the AddonRelease resource created by Terraform:
terraform destroy -
When prompted, enter
yesto confirm.Expected output:
... alicloud_cms_addon_release.default: Destroying... [id=************] alicloud_cms_addon_release.default: Destruction complete after 5s Destroy complete! Resources: 1 destroyed. -
Log in to the Alibaba Cloud console.
-
Verify that the integration configuration no longer appears in the list.
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_KEYandALICLOUD_SECRET_KEYenvironment 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_idparameter references an existing policy. Runterraform showto inspect the current policy ID. -
Workspace mismatch: Ensure that the
workspaceparameter 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 |