Deploy SQL Alarm Rule

Application Scenario

Huawei Cloud Log Tank Service (LTS) SQL alarm rule functionality allows users to set alarm conditions based on SQL query results, automatically triggering alarm notifications when query results meet preset conditions. By configuring SQL alarm rules, you can implement real-time log data monitoring, anomaly detection, and automated alerting, improving operation efficiency and system reliability.

This best practice is particularly suitable for scenarios that require real-time log data monitoring, system anomaly detection, and automated alert notification, such as application performance monitoring, error log alerts, business metric monitoring, security event detection, etc. This best practice will introduce how to use Terraform to automatically deploy LTS SQL alarm rules, including SMN topic, log group, log stream, and SQL alarm rule creation, implementing a complete log monitoring and alert solution.

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

Data Sources

Resources

Resource/Data Source Dependencies

Operation Steps

1. Script Preparation

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 SMN Topic

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an SMN topic resource:

Parameter Description:

  • name: SMN topic name, assigned by referencing the input variable topic_name

  • display_name: SMN topic display name, set to "The display name of topic"

3. Create Log Group

Add the following script to the TF file to instruct Terraform to create a log group resource:

Parameter Description:

  • group_name: Log group name, assigned by referencing the input variable group_name

  • ttl_in_days: Log expiration days for the log group, assigned by referencing the input variable group_log_expiration_days, default value is 14

4. Create Log Stream

Add the following script to the TF file to instruct Terraform to create a log stream resource:

Parameter Description:

  • group_id: Log group ID that the log stream belongs to, referencing the ID of the previously created log group resource

  • stream_name: Log stream name, assigned by referencing the input variable stream_name

  • ttl_in_days: Log expiration days for the log stream, assigned by referencing the input variable stream_log_expiration_days, default value is null (inherits log group settings)

5. Query LTS Notification Template Information

Add the following script to the TF file to instruct Terraform to query LTS notification template information:

Parameter Description:

  • count: Conditional creation, creates this data source when notification_template_name variable is not an empty string

  • domain_id: Domain ID, assigned by referencing the input variable domain_id, default value is null

6. Create SQL Alarm Rule

Add the following script to the TF file to instruct Terraform to create a SQL alarm rule resource:

Parameter Description:

  • name: SQL alarm rule name, assigned by referencing the input variable alarm_rule_name

  • condition_expression: Alarm condition expression, assigned by referencing the input variable alarm_rule_condition_expression

  • alarm_level: Alarm level, assigned by referencing the input variable alarm_rule_alarm_level, default value is "MINOR"

  • send_notifications: Whether to send notifications, set to true to enable notifications

  • trigger_condition_count: Trigger condition count, assigned by referencing the input variable alarm_rule_trigger_condition_count, default value is 2

  • trigger_condition_frequency: Trigger condition frequency, assigned by referencing the input variable alarm_rule_trigger_condition_frequency, default value is 3

  • send_recovery_notifications: Whether to send recovery notifications, assigned by referencing the input variable alarm_rule_send_recovery_notifications, default value is true

  • recovery_frequency: Recovery frequency, uses alarm_rule_recovery_frequency value when send_recovery_notifications is true

  • notification_frequency: Notification frequency, assigned by referencing the input variable alarm_rule_notification_frequency, default value is 15

  • alarm_rule_alias: Alarm rule alias, assigned by referencing the input variable alarm_rule_alias, default value is empty string

  • sql_requests: SQL request configuration block

    • title: Request title, assigned by referencing the input variable alarm_rule_request_title

    • sql: SQL query statement, assigned by referencing the input variable alarm_rule_request_sql

    • log_group_id: Log group ID, referencing the ID of the previously created log group resource

    • log_stream_id: Log stream ID, referencing the ID of the previously created log stream resource

    • search_time_range_unit: Search time range unit, assigned by referencing the input variable alarm_rule_request_search_time_range_unit, default value is "minute"

    • search_time_range: Search time range, assigned by referencing the input variable alarm_rule_request_search_time_range, default value is 5

    • log_group_name: Log group name, referencing the name of the previously created log group resource

    • log_stream_name: Log stream name, referencing the name of the previously created log stream resource

  • frequency: Frequency configuration block

    • type: Frequency type, assigned by referencing the input variable alarm_rule_frequency_type, default value is "HOURLY"

  • notification_save_rule: Notification save rule configuration block

    • template_name: Notification template name, prioritizes using notification_template_name variable, if empty then tries to get "sql_template" from query results

    • user_name: Notification user name, assigned by referencing the input variable alarm_rule_notification_user_name

    • language: Notification language, assigned by referencing the input variable alarm_rule_notification_language, default value is "en-us"

    • topics: Topic configuration block

      • name: Topic name, referencing the name of the previously created SMN topic resource

      • topic_urn: Topic URN, referencing the URN of the previously created SMN topic resource

      • display_name: Topic display name, referencing the display name of the previously created SMN topic resource

      • push_policy: Push policy, referencing the push policy of the previously created SMN topic resource

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

In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. 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:

  1. Save the above content as terraform.tfvars file in the working directory (this file name allows users to automatically import the content of this tfvars file when executing terraform commands; for other names, .auto needs to be added before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values as needed

  3. When executing terraform plan or terraform apply, Terraform will automatically read the variable values from this file

In addition to using terraform.tfvars file, variable values can also be set in the following ways:

  1. Command line parameters: terraform apply -var="group_name=my-group" -var="stream_name=my-stream"

  2. Environment variables: export TF_VAR_group_name=my-group

  3. Custom named variable files: terraform apply -var-file="custom.tfvars"

Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.

8. 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 the resource plan is correct, run terraform apply to start creating SQL alarm rules

  4. Run terraform show to view the created SQL alarm rule details

Reference Information

Last updated