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.
Related Resources/Data Sources
This best practice involves the following main resources:
Data Sources
Resources
Resource Dependencies
In this best practice, the following dependencies exist between resources:
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
ECS Instance depends on VPC Subnet and Security Group, which need to be created first
VPC Subnet depends on VPC, which needs to be created first
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:
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
ecs_image_idneeds to be replaced with the actual image IDWhen 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="resource_group_name=my_group" -var="vpc_name=my_vpc"Environment variables:
export TF_VAR_resource_group_name=my_groupandexport TF_VAR_vpc_name=my_vpcCustom 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:
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 the resource group and related resourcesRun
terraform showto 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