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.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Resources
Resource/Data Source Dependencies
huaweicloud_smn_topic
└── huaweicloud_ces_alarmruleNote: 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 Resources.
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_nameenterprise_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_namealarm_description: Alarm rule description, assigned by referencing the input variable
alarm_rule_description, optional parameteralarm_action_enabled: Whether to enable alarm actions, assigned by referencing the input variable
alarm_action_enabled, default istruealarm_enabled: Whether to enable the alarm, assigned by referencing the input variable
alarm_enabled, default istruealarm_type: Alarm type, assigned by referencing the input variable
alarm_type, default isALL_INSTANCEmetric: Monitoring metric configuration, namespace set to
SYS.SMNindicates monitoring SMN servicecondition: Alarm condition list, creates multiple alarm conditions through dynamic block
dynamic "condition"based on input variablealarm_rule_conditionsresources: Monitoring resource dimension list, creates resource dimensions through dynamic block
dynamic "resources"based on input variablealarm_rule_resourcealarm_actions: Alarm action configuration, type is
notification, notification list references SMN topic URNnotification_begin_time: Alarm notification start time, assigned by referencing the input variable
alarm_rule_notification_begin_time, optional parameternotification_end_time: Alarm notification stop time, assigned by referencing the input variable
alarm_rule_notification_end_time, optional parametereffective_timezone: Time zone, assigned by referencing the input variable
alarm_rule_effective_timezone, optional parameter
Note: Alarm rules reference the SMN topic
topic_urnthroughnotification_listinalarm_actionsto 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:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows Terraform to automatically import the variable values in thistfvarsfile when executing terraform commands. For other names, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs
When executing
terraform planorterraform 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:
Command-line parameters:
terraform apply -var="smn_topic_name=tf_test_topic" -var="alarm_rule_name=tf_test_alarm_rule"Environment variables:
export TF_VAR_smn_topic_name=tf_test_topicCustom-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:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming that the resource plan is correct, run
terraform applyto start creating SMN topic and CES alarm ruleRun
terraform showto view the created resources
Reference Information
Last updated