Cloud Eye Service (CES) alarm template is an alarm rule template function provided by the CES service, used to quickly create and manage alarm rules. By configuring alarm templates, you can define unified alarm policies, including monitoring metrics, alarm thresholds, alarm levels, etc., and then quickly create multiple alarm rules based on templates, improving the efficiency and consistency of alarm configuration. Automating CES alarm template creation through Terraform can ensure standardized and standardized alarm configuration, simplifying operational management. This best practice will introduce how to use Terraform to automatically create CES alarm templates.
Related Resources/Data Sources
This best practice involves the following main resources:
Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.
2. Create CES Alarm Template Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a CES alarm template resource:
variable"alarm_template_name"{description ="The name of the alarm template"type =string}variable"alarm_template_description"{description ="The description of the alarm template"type =stringdefault =""}variable"alarm_template_policies"{description ="The policy list of the CES alarm template"type =list(object({namespace=stringmetric_name=stringperiod=numberfilter=stringcomparison_operator=stringcount=numbersuppress_duration=numbervalue=numberalarm_level=numberunit=stringdimension_name=stringhierarchical_value=list(object({critical=numbermajor=numberminor=numberinfo=number}))}))default =[]}# Create CES alarm template resource in the specified region (defaults to the region specified in the provider block when region parameter is omitted)resource"huaweicloud_ces_alarm_template""test"{name =var.alarm_template_namedescription =var.alarm_template_descriptiondynamic"policies"{for_each =var.alarm_template_policiescontent{namespace =policies.value.namespacemetric_name =policies.value.metric_nameperiod =policies.value.periodfilter =policies.value.filtercomparison_operator =policies.value.comparison_operatorcount =policies.value.countsuppress_duration =policies.value.suppress_durationvalue =policies.value.valuealarm_level =policies.value.alarm_levelunit =policies.value.unitdimension_name =policies.value.dimension_namehierarchical_value{critical =policies.value.hierarchical_value[0].criticalmajor =policies.value.hierarchical_value[0].majorminor =policies.value.hierarchical_value[0].minorinfo =policies.value.hierarchical_value[0].info}}}}
Parameter Description:
name: The alarm template name, assigned by referencing the input variable alarm_template_name
description: The alarm template description, assigned by referencing the input variable alarm_template_description, optional parameter, default value is empty string
policies: The alarm policy list, assigned by referencing the input variable alarm_template_policies, each policy contains the following parameters:
namespace: Service namespace, such as SYS.APIG, SYS.ECS, etc.
metric_name: Alarm metric name
period: Alarm condition judgment period, unit is minutes
filter: Data aggregation method, such as average (average value), max (maximum value), min (minimum value), etc.
comparison_operator: Comparison condition for alarm threshold, such as >, >=, <, <=, =, etc.
count: Number of consecutive alarm triggering times
suppress_duration: Alarm suppression cycle, unit is seconds
value: Alarm threshold
alarm_level: Alarm level, 1-4 represent critical, major, minor, info respectively
unit: Unit string of alarm threshold
dimension_name: Resource dimension name
hierarchical_value: Multi-level alarm threshold configuration, contains the following fields:
critical: Threshold for critical level
major: Threshold for major level
minor: Threshold for minor level
info: Threshold for info level
3. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources use input variables to assign configuration content. These input parameters need to be manually entered during subsequent deployment. At the same time, Terraform provides a method to preset these configurations through tfvars files, which can avoid repeated input during each execution.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as a terraform.tfvars file in the working directory (this filename allows users to automatically import the content of this tfvars file when executing terraform commands. For other naming, you need to add .auto before tfvars, such as variables.auto.tfvars)
Modify parameter values according to actual needs
When executing terraform plan or terraform apply, Terraform will automatically read the variable values in this file
In addition to using the terraform.tfvars file, you can also set variable values in the following ways:
Command line parameters: terraform apply -var="alarm_template_name=my_template" -var="alarm_template_description=My description"
Environment variables: export TF_VAR_alarm_template_name=my_template and export TF_VAR_alarm_template_description=My description
Custom named variable file: terraform apply -var-file="custom.tfvars"
Note: If the same variable is set through multiple methods, Terraform will use variable values according to the following priority: command line parameters > variable file > environment variables > default values.
4. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create a CES alarm template:
Run terraform init to initialize the environment
Run terraform plan to view the resource creation plan
After confirming that the resource plan is correct, run terraform apply to start creating the alarm template
Run terraform show to view the details of the created alarm template
Note: After the alarm template is created, multiple alarm rules can be quickly created based on this template. The hierarchical_value in the alarm policy is used to configure multi-level alarm thresholds, and different thresholds can be set according to different alarm levels. Alarm levels 1-4 represent critical, major, minor, and info respectively, and you can choose the appropriate alarm level according to business needs.