Deploy Cloud Application Policy Group Scaling Policy

Application Scenario

Huawei Cloud Cloud Desktop (Workspace) is a cloud computing-based desktop virtualization service that provides enterprise users with secure and convenient cloud office solutions. Cloud application policy group scaling policies are an important component of the Workspace service's cloud application functionality, used to configure automatic scaling policies for cloud application server groups, automatically adjusting the number of server instances based on session usage to achieve elastic resource expansion and cost optimization.

Through cloud application policy group scaling policies, enterprises can automatically adjust the number of instances in cloud application server groups based on actual business load. When session usage exceeds the threshold, the system automatically scales out, and when session idle time reaches the set value, the system automatically scales in. This automatic scaling mechanism helps enterprises achieve on-demand resource allocation, improve resource utilization, reduce operating costs, and ensure user access experience. This best practice will introduce how to use Terraform to automatically deploy Workspace cloud application policy group scaling policies, including cloud application server group creation, cloud application group creation, policy group configuration, and scaling policy configuration.

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) 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 introduction in Preparation Before Deploying Huawei Cloud Resources for configuration introduction.

2. Query Workspace Service Information Through Data Source

Add the following script to the TF file (such as main.tf) to instruct Terraform to perform a data source query, the query results are used to create cloud application server groups:

Parameter Description:

  • This data source requires no additional parameters and automatically queries Workspace service information in the current region

3. Create Workspace Cloud Application Server Group

Add the following script to the TF file (such as main.tf) to instruct Terraform to create cloud application server group resources:

Parameter Description:

  • name: Cloud application server group name, assigned by referencing the input variable app_server_group_name

  • app_type: Application type, assigned by referencing the input variable app_server_group_app_type, default is "SESSION_DESKTOP_APP"

  • os_type: Operating system type, assigned by referencing the input variable app_server_group_os_type, default is "Windows"

  • flavor_id: Flavor ID, assigned by referencing the input variable app_server_group_flavor_id

  • image_type: Image type, fixed as "gold" (golden image)

  • image_id: Image ID, assigned by referencing the input variable app_server_group_image_id

  • image_product_id: Image product ID, assigned by referencing the input variable app_server_group_image_product_id

  • vpc_id: VPC ID, assigned based on the return results of the Workspace service query data source (data.huaweicloud_workspace_service)

  • subnet_id: Subnet ID, assigned based on the return results of the Workspace service query data source (data.huaweicloud_workspace_service)

  • system_disk_type: System disk type, assigned by referencing the input variable app_server_group_system_disk_type, default is "SAS"

  • system_disk_size: System disk size, assigned by referencing the input variable app_server_group_system_disk_size, default is 80GB

  • is_vdi: Whether it is VDI mode, fixed as true

4. Create Workspace Cloud Application Group

Add the following script to the TF file to instruct Terraform to create cloud application group resources:

Parameter Description:

  • depends_on: Explicit dependency declaration, ensuring that the cloud application server group is created before creating the cloud application group

  • server_group_id: The ID of the cloud application server group, referencing the ID of the cloud application server group resource created earlier

  • name: Cloud application group name, assigned by referencing the input variable app_group_name

  • type: Cloud application group type, fixed as "SESSION_DESKTOP_APP" indicating a session desktop application group

  • description: Cloud application group description, fixed as "Created APP group by Terraform"

5. Create Workspace Cloud Application Policy Group

Add the following script to the TF file to instruct Terraform to create cloud application policy group resources:

Parameter Description:

  • depends_on: Explicit dependency declaration, ensuring that the cloud application group is created before creating the policy group

  • name: Policy group name, assigned by referencing the input variable policy_group_name

  • priority: Policy group priority, assigned by referencing the input variable policy_group_priority, default is 1, smaller values indicate higher priority

  • description: Policy group description, assigned by referencing the input variable policy_group_description, default is "Created APP policy group by Terraform"

  • targets: Policy group target configuration block

    • id: Target ID, if target type is "APPGROUP" then use the cloud application group ID, otherwise use "default-apply-all-targets" to indicate applying to all targets

    • name: Target name, if target type is "APPGROUP" then use the cloud application group name, otherwise use "All-Targets"

    • type: Target type, assigned by referencing the input variable target_type, default is "APPGROUP" indicating applying to specified application group, "ALL" indicating applying to all targets

  • policies: Policy configuration, using jsonencode function to encode policy configuration as JSON string

    • client.automatic_reconnection_interval: Client automatic reconnection interval (minutes), assigned by referencing the input variable automatic_reconnection_interval, default is 10 minutes

    • client.session_persistence_time: Session persistence time (minutes), assigned by referencing the input variable session_persistence_time, default is 120 minutes

    • client.forbid_screen_capture: Whether to forbid screen capture, assigned by referencing the input variable forbid_screen_capture, default is true

6. Create Workspace Cloud Application Server Group Scaling Policy

Add the following script to the TF file to instruct Terraform to create cloud application server group scaling policy resources:

Parameter Description:

  • depends_on: Explicit dependency declaration, ensuring that the cloud application server group is created before creating the scaling policy

  • server_group_id: The ID of the cloud application server group, referencing the ID of the cloud application server group resource created earlier

  • max_scaling_amount: The maximum number of instances that can be scaled out, assigned by referencing the input variable max_scaling_amount, valid range is 1 to 100

  • single_expansion_count: The number of instances to scale out in a single scaling operation, assigned by referencing the input variable single_expansion_count, valid range is 1 to 10

  • scaling_policy_by_session: Scaling policy configuration block based on sessions

    • session_usage_threshold: Session usage threshold (percentage), assigned by referencing the input variable session_usage_threshold, default is 80%. When session usage exceeds this threshold, scaling out is triggered

    • shrink_after_session_idle_minutes: The number of minutes to wait before shrinking idle instances, assigned by referencing the input variable shrink_after_session_idle_minutes, default is 30 minutes. When session idle time reaches this value, scaling in is triggered

Note: The scaling policy automatically adjusts based on session usage. When session usage exceeds the threshold, the system automatically scales out instances; when session idle time reaches the set value, the system automatically scales in instances. This enables elastic resource expansion and cost optimization.

7. 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. 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 a terraform.tfvars file in the working directory (this filename allows users to automatically import the content of this tfvars file when executing terraform commands. For other names, you need to add .auto before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values according to actual needs

  3. When executing terraform plan or terraform 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:

  1. Command line parameters: terraform apply -var="app_server_group_name=my-server-group" -var="max_scaling_amount=10"

  2. Environment variables: export TF_VAR_app_server_group_name=my-server-group

  3. Custom 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.

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 that the resource plan is correct, run terraform apply to start creating cloud application server groups, cloud application groups, cloud application policy groups, and scaling policies

  4. Run terraform show to view the created cloud application policy group scaling policy details

Reference Information

Last updated