Deploy Scaling Group
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. Scaling group is the core resource of Auto Scaling service, used to manage a group of elastic computing instances with the same configuration and rules. By creating a scaling group, you can automatically manage the increase and decrease of instance numbers, realize elastic expansion and contraction of resources, and meet the needs of business load changes. This best practice will introduce how to use Terraform to automatically deploy a scaling group, including querying availability zones, instance flavors, and images, as well as creating security groups, key pairs, AS configuration, VPC network, and scaling group.
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 Scaling Group 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 scaling group related resources:
Parameter Description:
count: The number of data source creations, used to control whether to execute the availability zone list query, only when
var.availability_zoneis empty, the data source is created (i.e., execute the availability zone list query)
3. Query Instance Flavors Required for Scaling Group 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.configuration_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, if the availability zone is specified, use that value, otherwise use the first availability zone from the availability zone list query data source
performance_type: Performance type, assigned through input variable
configuration_flavor_performance_type, default is "normal" for standard typecpu_core_count: CPU core count, assigned through input variable
configuration_flavor_cpu_core_countmemory_size: Memory size (GB), assigned through input variable
configuration_flavor_memory_size
4. Query Images Required for Scaling Group 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.configuration_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 flavor ID is specified, use that value, otherwise use the first flavor ID from the instance flavor list query data source
visibility: Image visibility, assigned through input variable
configuration_image_visibility, default is "public" for public imagesos: Operating system type, assigned through input variable
configuration_image_os, default is "Ubuntu"
5. 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
6. 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)
7. 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)
security_group_ids: Security group ID list, assigned by referencing the ID of the security group resource (huaweicloud_networking_secgroup.test)
metadata: Instance metadata, assigned through input variable
configuration_metadata, used to inject custom data when instances startuser_data: User data script, assigned through input variable
configuration_user_data, used to execute initialization scripts when instances startdisk: Disk configuration block, creates multiple disk configurations through dynamic block (dynamic block) based on input variable
configuration_diskssize: Disk size (GB), assigned through
sizein input variableconfiguration_disksvolume_type: Volume type, assigned through
volume_typein input variableconfiguration_disksdisk_type: Disk type, assigned through
disk_typein input variableconfiguration_disks
public_ip: Public IP configuration block, creates public IP configuration through dynamic block (dynamic block) based on input variable
configuration_public_eip_settingseip: Elastic IP configuration block
ip_type: IP type, assigned through
ip_typein input variableconfiguration_public_eip_settingsbandwidth: Bandwidth configuration block
size: Bandwidth size (Mbps), assigned through
bandwidth.sizein input variableconfiguration_public_eip_settingsshare_type: Share type, assigned through
bandwidth.share_typein input variableconfiguration_public_eip_settingscharging_mode: Charging mode, assigned through
bandwidth.charging_modein input variableconfiguration_public_eip_settings
Note: The disk configuration must include at least one system disk (disk_type is "SYS"), and other disks are data disks. Public IP configuration is optional, if public IP is not needed, you can not configure
configuration_public_eip_settingsor set it to an empty list.
8. Create VPC Resource (Optional)
Add the following script to the TF file to inform Terraform to create VPC resources (if VPC ID is not specified):
Parameter Description:
count: The number of resource creations, used to control whether to create VPC resource, only when both
var.scaling_group_vpc_idandvar.scaling_group_subnet_idare empty, the VPC resource is createdname: The name of the VPC, assigned by referencing input variable
scaling_group_vpc_namecidr: The CIDR block of the VPC, assigned by referencing input variable
scaling_group_vpc_cidr, default is "192.168.0.0/16"
9. Create VPC Subnet Resource (Optional)
Add the following script to the TF file to inform Terraform to create VPC subnet resources (if subnet ID is not specified):
Parameter Description:
count: The number of resource creations, used to control whether to create VPC subnet resource, only when
var.scaling_group_subnet_idis empty, the VPC subnet resource is createdvpc_id: The VPC ID to which the subnet belongs, if the VPC ID is specified, use that value, otherwise assign by referencing the ID of the VPC resource (huaweicloud_vpc.test[0])
name: The name of the subnet, assigned by referencing input variable
scaling_group_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
10. Create AS Group Resource
Add the following script to the TF file to inform Terraform to create AS group resources:
Parameter Description:
scaling_group_name: The name of the AS group, assigned by referencing input variable
scaling_group_namescaling_configuration_id: AS configuration ID, assigned by referencing the ID of the AS configuration resource (huaweicloud_as_configuration.test)
desire_instance_number: The desired number of AS instances, assigned by referencing input variable
scaling_group_desire_instance_number, default is 2min_instance_number: The minimum number of AS instances, assigned by referencing input variable
scaling_group_min_instance_number, default is 0max_instance_number: The maximum number of AS instances, assigned by referencing input variable
scaling_group_max_instance_number, default is 10vpc_id: VPC ID, if the VPC ID is specified, use that value, otherwise assign by referencing the ID of the VPC resource (huaweicloud_vpc.test[0])
delete_publicip: Whether to delete the public IP address of AS instances when the AS group is deleted, assigned by referencing input variable
is_delete_scaling_group_publicip, default is truedelete_instances: Whether to delete AS instances when the AS group is deleted, assigned by referencing input variable
is_delete_scaling_group_instances, value is "yes" or "no", default is "yes"networks: Network configuration block, defines the subnet used by the AS group
id: Subnet ID, if the subnet ID is specified, use that value, otherwise assign by referencing the ID of the VPC subnet resource (huaweicloud_vpc_subnet.test[0])
Note:
min_instance_number,desire_instance_numberandmax_instance_numbermust satisfy the relationship:min_instance_number <= desire_instance_number <= max_instance_number.
11. 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="security_group_name=my-secgroup" -var="keypair_name=my-keypair"Environment variables:
export TF_VAR_security_group_name=my-secgroupCustom 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.
12. 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 scaling groupRun
terraform showto view the created scaling group
Reference Information
Last updated