Deploy Pay-per-Use Cloud Desktop

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 desktop provides remote desktop access capabilities, allowing users to access their cloud office environment anytime, anywhere through various terminal devices, while centrally managing data and applications to improve security and work efficiency. The pay-per-use billing mode allows enterprises to pay flexibly based on actual usage without prepaying large amounts of funds, suitable for temporary projects or scenarios with fluctuating usage. This best practice will introduce how to use Terraform to automatically deploy pay-per-use cloud desktop instances.

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. Query Availability Zones Required for Cloud Desktop Instance Resource Creation Through Data Source (data.huaweicloud_availability_zones)

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the results of which are used to create cloud desktop instances:

Parameter Description:

  • count: Creation count of the data source, used to control whether to execute the availability zone list query data source, only creates the data source when var.availability_zone is empty (i.e., executes availability zone list query)

3. Query Cloud Desktop Flavors Required for Cloud Desktop Instance Resource Creation Through Data Source (data.huaweicloud_workspace_flavors)

Add the following script to the TF file to instruct Terraform to query cloud desktop flavors that meet the conditions:

Parameter Description:

  • count: Creation count of the data source, used to control whether to execute the cloud desktop flavor list query data source, only creates the data source when var.desktop_flavor_id is empty (i.e., executes cloud desktop flavor list query)

  • os_type: Operating system type, optional values: Windows, Linux

  • vcpus: CPU core count, used to filter flavors

  • memory: Memory size (GB), used to filter flavors

  • availability_zone: Availability zone where the flavor is located, prioritizes using the availability zone specified in input variables, uses the first availability zone from data source query if not specified

4. Query Cloud Desktop Images Required for Cloud Desktop Instance Resource Creation Through Data Source (data.huaweicloud_images_images)

Add the following script to the TF file to instruct Terraform to query cloud desktop images that meet the conditions:

Parameter Description:

  • count: Creation count of the data source, used to control whether to execute the image list query data source, only creates the data source when var.desktop_image_id is empty (i.e., executes image list query)

  • name_regex: Regular expression for image names, used to filter cloud desktop related images

  • os: Operating system type, used to filter images

  • visibility: Image visibility, market indicates cloud market images

5. Query Cloud Desktop Service Status Through Data Source (data.huaweicloud_workspace_service)

Add the following script to the TF file to instruct Terraform to query the current cloud desktop service status:

Parameter Description: This data source is used to query the current cloud desktop service status. If the service status is "CLOSED", VPC, subnet, security group, and other network resources need to be created; if the service is already enabled, existing network resources can be reused.

6. Create VPC Resource (huaweicloud_vpc)

Add the following script to the TF file to instruct Terraform to conditionally create VPC resources based on cloud desktop service status:

Parameter Description:

  • count: Creation count of the resource, only creates VPC resource when cloud desktop service status is "CLOSED"

  • name: VPC name, assigned by referencing the input variable vpc_name

  • cidr: VPC CIDR block, assigned by referencing the input variable vpc_cidr

7. Create VPC Subnet Resource (huaweicloud_vpc_subnet)

Add the following script to the TF file to instruct Terraform to conditionally create VPC subnet resources based on cloud desktop service status:

Parameter Description:

  • count: Creation count of the resource, only creates subnet resource when cloud desktop service status is "CLOSED"

  • vpc_id: VPC ID that the subnet belongs to, referencing the ID of the previously created VPC resource

  • name: Subnet name, assigned by referencing the input variable subnet_name

  • cidr: Subnet CIDR block, uses cidrsubnet function to divide a subnet segment from VPC's CIDR block if not specified

  • gateway_ip: Subnet gateway IP, uses cidrhost function to get the first IP address from subnet segment as gateway IP if not specified

8. Create Cloud Desktop Service (huaweicloud_workspace_service)

Add the following script to the TF file to instruct Terraform to conditionally create cloud desktop service resources based on cloud desktop service status:

Parameter Description:

  • count: Creation count of the resource, only creates cloud desktop service resource when cloud desktop service status is "CLOSED"

  • access_mode: Access mode, using INTERNET indicates access through public network

  • vpc_id: VPC ID, referencing the ID of the previously created VPC resource

  • network_ids: Network ID list, referencing the ID of the previously created subnet resource

9. Create Security Group Resource (huaweicloud_networking_secgroup)

Add the following script to the TF file to instruct Terraform to conditionally create security group resources based on cloud desktop service status:

Parameter Description:

  • count: Creation count of the resource, only creates security group resource when cloud desktop service status is "CLOSED"

  • name: Security group name, assigned by referencing the input variable security_group_name

  • delete_default_rules: Whether to delete default rules, set to true to delete default rules

10. Create Security Group Rule Resource (huaweicloud_networking_secgroup_rule)

Add the following script to the TF file to instruct Terraform to conditionally create security group rule resources based on cloud desktop service status:

Parameter Description:

  • count: Creation count of the resource, only creates security group rule resource when cloud desktop service status is "CLOSED"

  • security_group_id: Security group ID, referencing the ID of the previously created security group resource

  • direction: Rule direction, egress indicates outbound traffic

  • ethertype: IP protocol version, IPv4 indicates IPv4 protocol

  • remote_ip_prefix: Remote IP address, 0.0.0.0/0 indicates allowing all IP addresses

  • priority: Rule priority, smaller values have higher priority

11. Create Cloud Desktop User (huaweicloud_workspace_user)

Add the following script to the TF file to instruct Terraform to create a cloud desktop user resource:

Parameter Description:

  • name: Username, assigned by referencing the input variable desktop_user_name

  • email: User email, assigned by referencing the input variable desktop_user_email

  • account_expires: Account expiration time, set to "0" indicates never expires

  • password_never_expires: Whether password never expires, set to false indicates password has expiration time

  • enable_change_password: Whether to allow password change, set to true indicates allowing password change

  • next_login_change_password: Whether to change password on next login, set to true indicates password change required on next login

  • disabled: Whether to disable user, set to false indicates user is not disabled

12. Create Cloud Desktop Instance (huaweicloud_workspace_desktop)

Add the following script to the TF file to instruct Terraform to create a cloud desktop instance resource:

Parameter Description:

  • flavor_id: Cloud desktop flavor ID, prioritizes using the flavor specified in input variables, uses the first non-flexus flavor from data source query if not specified

  • image_type: Image type, assigned by referencing the input variable desktop_image_visibility

  • image_id: Image ID, prioritizes using the image specified in input variables, uses the first image from data source query if not specified

  • availability_zone: Availability zone, prioritizes using the availability zone specified in input variables, uses the first availability zone from data source query if not specified

  • vpc_id: VPC ID, uses existing VPC or newly created VPC based on cloud desktop service status

  • security_groups: Security group ID list, uses different security group configurations based on cloud desktop service status

  • nic: Network interface configuration block (dynamic block), uses different network configurations based on cloud desktop service status

    • network_id: Unique identifier of the network, uses existing network or newly created subnet based on service status

  • name: Cloud desktop name, assigned by referencing the input variable cloud_desktop_name

  • user_name: Username, referencing the name of the previously created cloud desktop user resource

  • user_email: User email, referencing the email of the previously created cloud desktop user resource

  • user_group: User group name, assigned by referencing the input variable desktop_user_group_name, default is "users"

  • root_volume: System disk configuration block

    • type: Disk type, assigned by referencing the input variable desktop_root_volume_type, default is SSD

    • size: Disk size, assigned by referencing the input variable desktop_root_volume_size, default is 100GB

  • data_volume: Data disk configuration block (dynamic block)

    • type: Disk type, assigned by referencing the type value in the input variable desktop_data_volumes

    • size: Disk size, assigned by referencing the size value in the input variable desktop_data_volumes

  • lifecycle: Lifecycle management, ignores changes to flavor, image, and availability zone to avoid instance recreation

13. 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 content:

Usage:

  1. Save the above content as terraform.tfvars file in the working directory

  2. Modify parameter values as needed

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

For variables not specified in the terraform.tfvars file, Terraform will use the default values defined in the code or prompt the user for input during execution.

14. 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 cloud desktop instances

  4. Run terraform show to view the created cloud desktop instance details

Reference Information

Last updated