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.
Related Resources/Data Sources
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_zoneis 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_idis emptyavailability_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_idis emptyflavor_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:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows users to automatically import the content of thistfvarsfile when executing terraform commands. For other naming, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs, especially:
availability_zonecan be set to availability zone, if empty, it will be automatically queriedinstance_flavor_idcan be set to ECS flavor ID, if empty, it will be automatically queried based on CPU and memory parametersinstance_image_idcan be set to image ID, if empty, it will be automatically queried based on operating system typeinstance_flavor_performance_type,instance_flavor_cpu_core_count,instance_flavor_memory_sizecan be set to ECS flavor parametersinstance_image_os_type,instance_image_visibilitycan be set to image parameters
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 in the following ways:
Command line parameters:
terraform apply -var="vpc_name=my-vpc" -var="host_group_name=my-host-group"Environment variables:
export TF_VAR_vpc_name=my-vpcandexport TF_VAR_host_group_name=my-host-groupCustom 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:
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 VPC, subnet, security group, ECS instance, and HSS host groupRun
terraform showto 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