Deploy Alarm Policy
Application Scenario
Huawei Cloud Auto Scaling service is a service that automatically adjusts computing resources, capable of automatically adjusting the number of elastic computing instances based on business needs and policies. Alarm policy is an automatic scaling policy based on monitoring metrics. By configuring CES (Cloud Eye Service) alarm rules, when monitoring metrics reach the set threshold, scaling policies are automatically triggered to perform scaling operations. This best practice will introduce how to use Terraform to automatically deploy alarm policies, including the creation of VPC network, security groups, key pairs, AS configuration, AS group, SMN topic, CES alarm rules, and alarm policies.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Implementation 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 introduction, refer to the introduction in Preparation Before Deploying Huawei Cloud Resources.
2. Query Availability Zones Required for Alarm Policy Resource Creation Through Data Source
Add the following script to the TF file (such as main.tf) to inform Terraform to perform a data source query, the query results are used to create alarm policy related resources:
Parameter Description: This data source does not require additional parameters and queries all available availability zone information in the current region by default.
3. Query Instance Flavors Required for Alarm Policy Resource Creation Through Data Source
Add the following script to the TF file to inform Terraform to query instance flavors that meet the conditions:
Parameter Description:
count: The number of data source creations, used to control whether to execute the instance flavor list query, only when
var.instance_flavor_idis empty, the data source is created (i.e., execute the instance flavor list query)availability_zone: The availability zone where the instance flavor is located, using the first availability zone from the availability zone list query data source
performance_type: Performance type, assigned through input variable
instance_flavor_performance_type, default is "normal" for standard typecpu_core_count: CPU core count, assigned through input variable
instance_flavor_cpu_core_countmemory_size: Memory size (GB), assigned through input variable
instance_flavor_memory_size
4. Query Images Required for Alarm Policy Resource Creation Through Data Source
Add the following script to the TF file to inform Terraform to query images that meet the conditions:
Parameter Description:
count: The number of data source creations, used to control whether to execute the image list query, only when
var.instance_image_idis empty, the data source is created (i.e., execute the image list query)flavor_id: The flavor ID supported by the image, if the instance flavor ID is specified, use that ID, otherwise use the first flavor ID from the instance flavor list query data source
visibility: Image visibility, set to "public" for public images
os: Operating system type, set to "Ubuntu"
5. Create VPC Resource
Add the following script to the TF file to inform Terraform to create VPC resources:
Parameter Description:
name: The name of the VPC, assigned by referencing input variable
vpc_namecidr: The CIDR block of the VPC, assigned by referencing input variable
vpc_cidr, default is "192.168.0.0/16"
6. Create VPC Subnet Resource
Add the following script to the TF file to inform Terraform to create VPC subnet resources:
Parameter Description:
vpc_id: The VPC ID to which the subnet belongs, assigned by referencing the ID of the VPC resource (huaweicloud_vpc.test)
name: The name of the subnet, assigned by referencing input variable
subnet_namecidr: The CIDR block of the subnet, if the subnet CIDR is specified, use that value, otherwise automatically calculate based on the VPC's CIDR block using the
cidrsubnetfunctiongateway_ip: The gateway IP of the subnet, if the gateway IP is specified, use that value, otherwise automatically calculate based on the subnet CIDR using the
cidrhostfunction
7. Create Security Group Resource
Add the following script to the TF file to inform Terraform to create security group resources:
Parameter Description:
name: The name of the security group, assigned by referencing input variable
security_group_namedelete_default_rules: Whether to delete default rules, set to true to delete default rules
8. Create Key Pair Resource
Add the following script to the TF file to inform Terraform to create key pair resources:
Parameter Description:
name: The name of the key pair, assigned by referencing input variable
keypair_namepublic_key: The public key of the key pair, if the public key is specified, use that value, otherwise set to null (the system will automatically generate a key pair)
9. Create AS Configuration Resource
Add the following script to the TF file to inform Terraform to create AS configuration resources:
Parameter Description:
scaling_configuration_name: The name of the AS configuration, assigned by referencing input variable
configuration_nameinstance_config: Instance configuration block, defines the configuration for AS instances
image: Instance image ID, if the image ID is specified, use that value, otherwise use the first image ID from the image list query data source
flavor: Instance flavor ID, if the flavor ID is specified, use that value, otherwise use the first flavor ID from the instance flavor list query data source
key_name: Key pair ID, assigned by referencing the ID of the key pair resource (huaweicloud_kps_keypair.test)
disk: Disk configuration block, creates multiple disk configurations through dynamic block (dynamic block) based on input variable
disk_configurationsdisk_type: Disk type, assigned through
disk_typein input variabledisk_configurationsvolume_type: Volume type, assigned through
volume_typein input variabledisk_configurationssize: Disk size (GB), assigned through
volume_sizein input variabledisk_configurations
Note: The disk configuration must include exactly one system disk (disk_type is "SYS"), and other disks are data disks.
10. Create AS Group Resource
Add the following script to the TF file to inform Terraform to create AS group resources:
Parameter Description:
scaling_configuration_id: AS configuration ID, assigned by referencing the ID of the AS configuration resource (huaweicloud_as_configuration.test)
vpc_id: VPC ID, assigned by referencing the ID of the VPC resource (huaweicloud_vpc.test)
scaling_group_name: The name of the AS group, assigned by referencing input variable
group_namedesire_instance_number: The desired number of AS instances, assigned by referencing input variable
desire_instance_numbermin_instance_number: The minimum number of AS instances, assigned by referencing input variable
min_instance_numbermax_instance_number: The maximum number of AS instances, assigned by referencing input variable
max_instance_numberdelete_publicip: Whether to delete the public IP address of AS instances when the AS group is deleted, assigned by referencing input variable
is_delete_publicipdelete_instances: Whether to delete AS instances when the AS group is deleted, assigned by referencing input variable
is_delete_instances, value is "yes" or "no"networks: Network configuration block, defines the subnet used by the AS group
id: Subnet ID, assigned by referencing the ID of the VPC subnet resource (huaweicloud_vpc_subnet.test)
security_groups: Security group configuration block, defines the security group used by the AS group
id: Security group ID, assigned by referencing the ID of the security group resource (huaweicloud_networking_secgroup.test)
11. Create SMN Topic Resource
Add the following script to the TF file to inform Terraform to create SMN topic resources:
Parameter Description:
name: The name of the SMN topic, assigned by referencing input variable
topic_name
12. Create CES Alarm Rule Resource
Add the following script to the TF file to inform Terraform to create CES alarm rule resources:
Parameter Description:
alarm_name: The name of the alarm rule, assigned by referencing input variable
alarm_rule_namemetric: Monitoring metric configuration block
namespace: Namespace, set to "SYS.AS" for Auto Scaling service monitoring metrics
resources: Monitoring resource configuration block
dimensions: Dimension configuration block, defines dimension information of monitoring resources
name: Dimension name, set to "AutoScalingGroup" for monitoring AS group
value: Dimension value, assigned by referencing the ID of the AS group resource (huaweicloud_as_group.test)
condition: Alarm condition configuration block, creates multiple alarm conditions through dynamic block (dynamic block) based on input variable
rule_conditionsalarm_level: Alarm level, assigned through
alarm_levelin input variablerule_conditions, default is 2 (important)metric_name: Monitoring metric name, assigned through
metric_namein input variablerule_conditions, for example "cpu_util" for CPU utilizationperiod: Monitoring period (seconds), assigned through
periodin input variablerule_conditionsfilter: Aggregation method, assigned through
filterin input variablerule_conditions, for example "average" for average valuecomparison_operator: Comparison operator, assigned through
comparison_operatorin input variablerule_conditions, for example ">" for greater thansuppress_duration: Suppression duration (seconds), assigned through
suppress_durationin input variablerule_conditions, default is 0value: Threshold, assigned through
valuein input variablerule_conditionscount: Continuous trigger count, assigned through
countin input variablerule_conditions
alarm_actions: Alarm action configuration block
type: Action type, set to "autoscaling" for triggering Auto Scaling
notification_list: Notification list, assigned by referencing the ID of the SMN topic resource (huaweicloud_smn_topic.test)
13. Create AS Scaling Policy Resources
Add the following script to the TF file to inform Terraform to create AS scaling policy resources:
Parameter Description (Scaling Up Policy):
scaling_policy_name: The name of the scaling up policy, assigned by referencing input variable
scaling_up_policy_namescaling_policy_type: Policy type, set to "ALARM" for alarm-based policy
scaling_group_id: AS group ID, assigned by referencing the ID of the AS group resource (huaweicloud_as_group.test)
alarm_id: Alarm rule ID, assigned by referencing the ID of the CES alarm rule resource (huaweicloud_ces_alarmrule.test)
cool_down_time: Cool down time (seconds), assigned by referencing input variable
scaling_up_cool_down_time, default is 300 secondsscaling_policy_action: Policy action configuration block
operation: Operation type, set to "ADD" for adding instances
instance_number: Instance count, assigned by referencing input variable
scaling_up_instance_number, indicates the number of instances to add each time when scaling up
Parameter Description (Scaling Down Policy):
scaling_policy_name: The name of the scaling down policy, assigned by referencing input variable
scaling_down_policy_namescaling_policy_type: Policy type, set to "ALARM" for alarm-based policy
scaling_group_id: AS group ID, assigned by referencing the ID of the AS group resource (huaweicloud_as_group.test)
alarm_id: Alarm rule ID, assigned by referencing the ID of the CES alarm rule resource (huaweicloud_ces_alarmrule.test)
cool_down_time: Cool down time (seconds), assigned by referencing input variable
scaling_down_cool_down_time, default is 300 secondsscaling_policy_action: Policy action configuration block
operation: Operation type, set to "REMOVE" for removing instances
instance_number: Instance count, assigned by referencing input variable
scaling_down_instance_number, indicates the number of instances to remove each time when scaling down
14. Preset Input Parameters Required for Resource Deployment
In this practice, some resources and data sources use input variables to assign configuration content, and 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, example content is as follows:
Usage:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows users to automatically import the content in thetfvarsfile when executing terraform commands, other naming requires adding.autobefore tfvars, such asvariables.auto.tfvars)Modify the 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="vpc_name=my-vpc" -var="subnet_name=my-subnet"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom 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.
15. 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 alarm policiesRun
terraform showto view the created alarm policies
Reference Information
Last updated