Deploy Notification
Application Scenario
The notification function of Cloud Trace Service (CTS) is an event-driven alert mechanism that can monitor cloud resource operations and send real-time notifications. Through CTS notifications, you can achieve security monitoring, abnormal alerting, operation auditing, real-time notification, and other functions.
CTS notifications are particularly suitable for scenarios that require real-time monitoring of cloud resource operations, conducting security alerting, implementing automated responses, etc., such as security event monitoring, abnormal operation alerting, compliance checks, operation automation, etc. This best practice will introduce how to use Terraform to automatically deploy a CTS notification configuration, implementing real-time monitoring and notification of cloud resource operations.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
This best practice does not use data sources.
Resources
Resource/Data Source Dependencies
huaweicloud_smn_topic
└── huaweicloud_cts_notificationOperation 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
3. Create CTS Notification
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a CTS notification resource:
Parameter Description:
name: CTS notification name, assigned by referencing the input variable notification_name
operation_type: Operation type, assigned by referencing the input variable notification_operation_type, default value "customized" means custom operations
smn_topic: Associated SMN topic ID, assigned by referencing huaweicloud_smn_topic.test.id
agency_name: IAM agency name that allows CTS to access SMN resources, assigned by referencing the input variable notification_agency_name
filter: Notification filter, created using dynamic blocks, containing the following parameters:
condition: Filter condition, supports logical operators like "AND", "OR"
rule: Filter rule list, supports filtering based on status code, resource name, API version, etc.
operations: Monitored operations, created using dynamic blocks, containing the following parameters:
service: Cloud service name, such as "ECS", "VPC", etc.
resource: Resource type, such as "ecs", "vpc", etc.
trace_names: List of tracked operation names, such as "createServer", "deleteServer", etc.
operation_users: Operation users, created using dynamic blocks, containing the following parameters:
group: User group name
users: List of user names
4. 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:
Save the above content as
terraform.tfvarsfile in the working directory (this file name allows users to automatically import the content of thistfvarsfile when executing terraform commands; for other names,.autoneeds to be added before tfvars, such asvariables.auto.tfvars)Modify parameter values as needed
When executing
terraform planorterraform 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:
Command line parameters:
terraform apply -var="notification_name=my-notification" -var="topic_name=my-topic"Environment variables:
export TF_VAR_notification_name=my-notificationCustom 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.
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 the resource plan is correct, run
terraform applyto start creating SMN topic and CTS notificationRun
terraform showto view the details of the created SMN topic and CTS notification
Reference Information
Last updated