Deploy CES Event Alarm Rule

Application Scenario

Simple Message Notification (SMN) is a reliable and scalable message notification service provided by Huawei Cloud, supporting multiple message notification methods including email, SMS, HTTP/HTTPS, etc. Cloud Eye Service (CES) is a monitoring service provided by Huawei Cloud, supporting real-time resource monitoring and alarms. Through CES event alarm rules, SMN topic events can be monitored, and notifications are automatically sent when alarm conditions are met. This best practice introduces how to use Terraform to automatically deploy CES event alarm rules, including creating SMN topics and configuring CES alarm rules, achieving monitoring and alerting for SMN topic events.

This best practice involves the following main resources and data sources:

Resources

Resource/Data Source Dependencies

huaweicloud_smn_topic
    └── huaweicloud_ces_alarmrule

Note: CES alarm rules need to reference the SMN topic URN to send alarm notifications, so alarm rules depend on SMN topic resources.

Operation Steps

1. Script Preparation

Prepare the TF file (such as main.tf) for writing the current best practice script in the specified workspace, 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. For configuration details, refer to the introduction in Preparation Before Deploying Huawei Cloud Resourcesarrow-up-right.

2. Create SMN Topic

Add the following script to the TF file (such as main.tf) to create an SMN topic:

Parameter Description:

  • name: Topic name, assigned by referencing the input variable smn_topic_name

  • enterprise_project_id: Enterprise project ID, assigned by referencing the input variable enterprise_project_id, optional parameter

3. Create CES Alarm Rule

Add the following script to the TF file (such as main.tf) to create a CES alarm rule:

Parameter Description:

  • alarm_name: Alarm rule name, assigned by referencing the input variable alarm_rule_name

  • alarm_description: Alarm rule description, assigned by referencing the input variable alarm_rule_description, optional parameter

  • alarm_action_enabled: Whether to enable alarm actions, assigned by referencing the input variable alarm_action_enabled, default is true

  • alarm_enabled: Whether to enable the alarm, assigned by referencing the input variable alarm_enabled, default is true

  • alarm_type: Alarm type, assigned by referencing the input variable alarm_type, default is ALL_INSTANCE

  • metric: Monitoring metric configuration, namespace set to SYS.SMN indicates monitoring SMN service

  • condition: Alarm condition list, creates multiple alarm conditions through dynamic block dynamic "condition" based on input variable alarm_rule_conditions

  • resources: Monitoring resource dimension list, creates resource dimensions through dynamic block dynamic "resources" based on input variable alarm_rule_resource

  • alarm_actions: Alarm action configuration, type is notification, notification list references SMN topic URN

  • notification_begin_time: Alarm notification start time, assigned by referencing the input variable alarm_rule_notification_begin_time, optional parameter

  • notification_end_time: Alarm notification stop time, assigned by referencing the input variable alarm_rule_notification_end_time, optional parameter

  • effective_timezone: Time zone, assigned by referencing the input variable alarm_rule_effective_timezone, optional parameter

Note: Alarm rules reference the SMN topic topic_urn through notification_list in alarm_actions to send alarm notifications. Alarm conditions support multiple condition configurations, each condition includes metric name, period, filter method, comparison operator, threshold, and other parameters.

4. Preset Input Parameters Required for Resource Deployment (Optional)

In this practice, some resources and data sources use input variables to assign configuration content. These input parameters need to be manually entered during subsequent deployment. Terraform also provides a method to preset these configurations through tfvars files, which can avoid repeated input each time.

Create a terraform.tfvars file in the working directory with the following example content:

Usage:

  1. Save the above content as a terraform.tfvars file in the working directory (this filename allows Terraform to automatically import the variable values in this tfvars file when executing terraform commands. For other names, you need to add .auto before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values according to actual needs

  3. 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 through the following methods:

  1. Command-line parameters: terraform apply -var="smn_topic_name=tf_test_topic" -var="alarm_rule_name=tf_test_alarm_rule"

  2. Environment variables: export TF_VAR_smn_topic_name=tf_test_topic

  3. Custom-named variable files: 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 files > environment variables > default values.

5. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create resources:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming that the resource plan is correct, run terraform apply to start creating SMN topic and CES alarm rule

  4. Run terraform show to view the created resources

Reference Information

Last updated