Deploy Resource Group

Application Scenario

Cloud Eye Service (CES) resource group is a resource grouping management function provided by the CES service, used to group multiple cloud resources according to business needs. By configuring resource groups, you can organize related cloud resources (such as ECS instances, RDS instances, etc.) together for unified monitoring, alarm and operation management, improving the efficiency and convenience of resource management. Automating CES resource group creation through Terraform can ensure standardized and consistent resource grouping configuration, simplifying operational management. This best practice will introduce how to use Terraform to automatically create CES resource groups.

This best practice involves the following main resources:

Data Sources

Resources

Resource Dependencies

In this best practice, the following dependencies exist between resources:

  1. CES Resource Group depends on cloud resources such as ECS Instance, which need to be created first before they can be added to the resource group

  2. ECS Instance depends on VPC Subnet and Security Group, which need to be created first

  3. VPC Subnet depends on VPC, which needs to be created first

  4. ECS Instance depends on Availability Zones Data Source and ECS Flavors Data Source to obtain availability zone and flavor information

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

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

Parameter Description:

  • availability_zone: Availability zone name, obtained by referencing the availability zones data source

  • performance_type: ECS flavor performance type, assigned by referencing the input variable ecs_flavor_performance_type, default value is "normal"

  • cpu_core_count: ECS flavor CPU core count, assigned by referencing the input variable ecs_flavor_cpu_core_count, default value is 2

  • memory_size: ECS flavor memory size, assigned by referencing the input variable ecs_flavor_memory_size, default value is 4

3. Create Basic Resources

Add the following script to the TF file (e.g., main.tf) to create VPC, subnet, security group and ECS instance:

4. Create CES Resource Group Resource

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a CES resource group resource:

Parameter Description:

  • name: The resource group name, assigned by referencing the input variable resource_group_name

  • resources: The resource list, assigned by referencing the input variable resource_group_resources, optional parameter, default value is empty list, each resource contains the following parameters:

    • namespace: Service namespace, such as SYS.ECS, SYS.RDS, etc.

    • dimensions: Resource dimension list, each dimension contains the following fields:

      • name: Dimension name, such as instance_id, rds_instance_id, etc.

      • value: Dimension value, such as ECS instance ID, RDS instance ID, etc.

5. Preset Input Parameters Required for Resource Deployment (Optional)

In this practice, some resources 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 ecs_image_id needs to be replaced with the actual image ID

  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="resource_group_name=my_group" -var="vpc_name=my_vpc"

  2. Environment variables: export TF_VAR_resource_group_name=my_group and export TF_VAR_vpc_name=my_vpc

  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.

6. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create a CES resource group:

  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 the resource group and related resources

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

Note: After the resource group is created, multiple cloud resources can be added to the resource group for unified management. Resource groups support resource filtering by namespace and dimensions, allowing flexible organization and management of cloud resources. After the resource group is created, you can further configure it in the CES console, such as creating alarm rules, viewing monitoring data, etc.

Reference Information

Last updated