Deploy Host Group

Application Scenario

Host Security Service (HSS) is a host security protection service provided by Huawei Cloud, offering asset management, vulnerability management, intrusion detection, baseline checks, and other functions to help you comprehensively protect the security of cloud hosts. By creating HSS host groups, you can group multiple hosts for management, uniformly configure security policies, perform security checks, and conduct security operations, improving the efficiency and convenience of host security management. This best practice will introduce how to use Terraform to automatically deploy HSS host groups, including VPC, subnet, security group, ECS instance (with HSS agent), and HSS host group creation.

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 Data Source

Add the following script to the TF file (e.g., main.tf) to query availability zone information:

Parameter Description:

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

3. Query ECS Flavors Data Source

Add the following script to the TF file (e.g., main.tf) to query ECS flavor information:

Parameter Description:

  • count: The number of data source creations, used to control whether to execute the ECS flavor list query data source, only creates the data source (i.e., executes the ECS flavor list query) when var.instance_flavor_id is empty

  • availability_zone: Availability zone, assigned by referencing input variables or availability zone data source

  • performance_type: Performance type, assigned by referencing input variable instance_flavor_performance_type, default value is "normal"

  • cpu_core_count: Number of CPU cores, assigned by referencing input variable instance_flavor_cpu_core_count, default value is 2

  • memory_size: Memory size (GB), assigned by referencing input variable instance_flavor_memory_size, default value is 4

4. Query Images Data Source

Add the following script to the TF file (e.g., main.tf) to query image information:

Parameter Description:

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

  • flavor_id: Flavor ID, assigned by referencing input variables or ECS flavor data source

  • os: Operating system type, assigned by referencing input variable instance_image_os_type, default value is "Ubuntu"

  • visibility: Image visibility, assigned by referencing input variable instance_image_visibility, default value is "public"

5. Create VPC Resource

Add the following script to the TF file (e.g., main.tf) to create a VPC:

Parameter Description:

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

  • cidr: VPC CIDR block, assigned by referencing input variable vpc_cidr, default value is "192.168.0.0/16"

6. Create VPC Subnet Resource

Add the following script to the TF file (e.g., main.tf) to create a VPC subnet:

Parameter Description:

  • vpc_id: VPC ID, assigned by referencing the VPC resource

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

  • cidr: Subnet CIDR block, assigned by referencing input variables or automatic calculation

  • gateway_ip: Subnet gateway IP, assigned by referencing input variables or automatic calculation

7. Create Security Group Resource

Add the following script to the TF file (e.g., main.tf) to create a security group:

Parameter Description:

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

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

8. Create ECS Instance Resource

Add the following script to the TF file (e.g., main.tf) to create an ECS instance:

Parameter Description:

  • name: ECS instance name, assigned by referencing input variable ecs_instance_name

  • image_id: Image ID, assigned by referencing input variables or image data source

  • flavor_id: Flavor ID, assigned by referencing input variables or ECS flavor data source

  • availability_zone: Availability zone, assigned by referencing input variables or availability zone data source

  • security_group_ids: Security group ID list, assigned by referencing the security group resource

  • agent_list: Agent list, set to "hss" to install HSS agent

  • network.uuid: Network subnet ID, assigned by referencing the VPC subnet resource

Note: ECS instances need to configure agent_list = "hss" to install HSS agent, which is a prerequisite for creating HSS host groups. After the ECS instance is created, you need to wait for the HSS agent installation to complete before adding the instance to the HSS host group.

9. Create HSS Host Group Resource

Add the following script to the TF file (e.g., main.tf) to create an HSS host group:

Parameter Description:

  • name: HSS host group name, assigned by referencing input variable host_group_name

  • host_ids: Host ID list, assigned by referencing the ECS instance resource

Note: HSS host groups need to include ECS instances that have HSS agent installed. Ensure that the ECS instance has been created and the HSS agent installation is complete before adding the instance to the host group.

10. 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 naming, you need to add .auto before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values according to actual needs, especially:

    • availability_zone can be set to availability zone, if empty, it will be automatically queried

    • instance_flavor_id can be set to ECS flavor ID, if empty, it will be automatically queried based on CPU and memory parameters

    • instance_image_id can be set to image ID, if empty, it will be automatically queried based on operating system type

    • instance_flavor_performance_type, instance_flavor_cpu_core_count, instance_flavor_memory_size can be set to ECS flavor parameters

    • instance_image_os_type, instance_image_visibility can be set to image parameters

  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 in the following ways:

  1. Command line parameters: terraform apply -var="vpc_name=my-vpc" -var="host_group_name=my-host-group"

  2. Environment variables: export TF_VAR_vpc_name=my-vpc and export TF_VAR_host_group_name=my-host-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. ECS instances need to configure agent_list = "hss" to install HSS agent, which is a prerequisite for creating HSS host groups.

11. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create HSS host groups:

  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 VPC, subnet, security group, ECS instance, and HSS host group

  4. Run terraform show to view the details of the created HSS host group

Note: After the ECS instance is created, you need to wait for the HSS agent installation to complete before adding the instance to the HSS host group. If the instance has not completed HSS agent installation, host group creation may fail. It is recommended to confirm the HSS agent status of the ECS instance before creating the host group.

Reference Information

Last updated