Cloud Eye Service (CES) dashboard is a monitoring data visualization function provided by the CES service, used to centrally display multiple monitoring metrics and resource status. By configuring dashboards, you can create custom monitoring views, centrally display multiple monitoring metrics in the form of charts, tables, etc., making it convenient for operation personnel to quickly understand the overall running status of cloud resources. Automating CES dashboard creation through Terraform can ensure standardized and consistent monitoring view configuration, improving operational efficiency. This best practice will introduce how to use Terraform to automatically create CES dashboards.
Related Resources/Data Sources
This best practice involves the following main resources:
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. Create CES Dashboard Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a CES dashboard resource:
variable"dashboard_name"{description ="The name of the alarm dashboard"type =string}variable"dashboard_row_widget_num"{description ="The monitoring view display mode"type =number}variable"dashboard_extend_info"{description ="The information about the extension"type =list(object({filter=stringperiod=stringdisplay_time=numberrefresh_time=numberfrom=numberto=numberscreen_color=stringenable_screen_auto_play=booltime_interval=numberenable_legend=boolfull_screen_widget_num=number}))default =[]}variable"dashboard_id"{description ="The copied dashboard ID"type =stringdefault =null}variable"enterprise_project_id"{description ="The enterprise project ID of the dashboard"type =stringdefault =null}variable"is_favorite"{description ="Whether the dashboard is favorite"type =booldefault =false}# Create CES dashboard resource in the specified region (defaults to the region specified in the provider block when region parameter is omitted)resource"huaweicloud_ces_dashboard""test"{name =var.dashboard_namerow_widget_num =var.dashboard_row_widget_numdynamic"extend_info"{for_each =length(var.dashboard_extend_info)>0? var.dashboard_extend_info:[]content{filter =extend_info.value.filterperiod =extend_info.value.perioddisplay_time =extend_info.value.display_timerefresh_time =extend_info.value.refresh_timefrom =extend_info.value.fromto =extend_info.value.toscreen_color =extend_info.value.screen_colorenable_screen_auto_play =extend_info.value.enable_screen_auto_playtime_interval =extend_info.value.time_intervalenable_legend =extend_info.value.enable_legendfull_screen_widget_num =extend_info.value.full_screen_widget_num}}dashboard_id =var.dashboard_identerprise_project_id =var.enterprise_project_idis_favorite =var.is_favorite}
Parameter Description:
name: The dashboard name, assigned by referencing the input variable dashboard_name
row_widget_num: The monitoring view display mode, assigned by referencing the input variable dashboard_row_widget_num
extend_info: The extension information list, assigned by referencing the input variable dashboard_extend_info, optional parameter, default value is empty list, each extension information contains the following parameters:
filter: Metric aggregation method
period: Metric aggregation period
display_time: Display time
refresh_time: Refresh time
from: Start time
to: End time
screen_color: Monitoring screen background color
enable_screen_auto_play: Whether to enable monitoring screen automatic switching
time_interval: Monitoring screen automatic switching time interval
enable_legend: Whether to enable legend
full_screen_widget_num: Number of large screen display views
dashboard_id: The copied dashboard ID, assigned by referencing the input variable dashboard_id, optional parameter, default value is null
enterprise_project_id: The enterprise project ID of the dashboard, assigned by referencing the input variable enterprise_project_id, optional parameter, default value is null
is_favorite: Whether the dashboard is favorite, assigned by referencing the input variable is_favorite, optional parameter, default value is false
3. 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.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)
Modify parameter values according to actual needs
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:
Command line parameters: terraform apply -var="dashboard_name=my_dashboard" -var="dashboard_row_widget_num=2"
Environment variables: export TF_VAR_dashboard_name=my_dashboard and export TF_VAR_dashboard_row_widget_num=2
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.
4. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create a CES dashboard:
Run terraform init to initialize the environment
Run terraform plan to view the resource creation plan
After confirming that the resource plan is correct, run terraform apply to start creating the dashboard
Run terraform show to view the details of the created dashboard
Note: After the dashboard is created, you can further configure it in the CES console, adding monitoring metrics and charts. By setting extend_info, you can configure the display parameters of the monitoring screen, including automatic switching, refresh time, etc. The dashboard supports favorite function, making it convenient to quickly access commonly used monitoring views.