Help Center/ Cloud Container Engine/ Best Practices/ Terraform/ Installing the AutoScaler Add-on Using Terraform
Updated on 2026-08-20 GMT+08:00

Installing the AutoScaler Add-on Using Terraform

The CCE cluster AutoScaler add-on is the core component for automatic node scaling. It works with node pools to automatically add nodes when traffic increases and remove nodes when resources are idle. By using Terraform to automatically deploy add-ons, you can incorporate add-on installation into the infrastructure as code (IaC) system for batch deployment in multiple environments and unified version control.

This section uses the AutoScaler add-on as an example to describe how to use Terraform to automatically install and deploy add-ons in a CCE cluster.

Prerequisites

Procedure

Step 1: Create a Project Directory and Initialize the Configuration

  1. Create an add-on management directory (the directory name is user-defined).
    mkdir terraform_cce_addon
    cd ./terraform_cce_addon
  2. Copy the prepared provider configuration file to the current directory. The init.tf file has been created in the terraform_hw_demo directory in section "Installing Terraform and Configuring the Huawei Cloud Provider".
    cp ../terraform_hw_demo/init.tf .

Step 2: Create Configuration Files

All the following configuration files are created in the project directory.

  1. Query the CCE cluster ID.

    Create the data_cce_cluster.tf file to query the cluster ID based on the cluster name. The cluster ID will be used to specify the cluster that the node pool belongs to during node pool creation.

    variable "cluster_name" {
      description = "CCE cluster name"
      type        = string
    }
    
    data "huaweicloud_cce_cluster" "cluster" {
      name   = var.cluster_name
      status = "Available"
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    name

    CCE cluster name, which must be the same as the name of the created cluster.

    status

    Cluster status. Available indicates that only available clusters are queried.

  2. Query project information.

    Create the data_project.tf file and query the IAM project information in the specified region. The information is used for the tenant_id parameter in the add-on configuration.

    variable "project_id" {
      description = "Project ID"
      type        = string
      default     = ""
    }
    
    variable "region_name" {
      description = "Region name"
      type        = string
    }
    
    # Obtain the IAM project information that meets the conditions in the specified region (if the region parameter is not specified, the region specified in the provider block is used by default). The information is used to create add-on-related resources.
    data "huaweicloud_identity_projects" "test" {
      name = var.region_name
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    name

    Region name.

  3. Obtain the add-on template.

    Create the data_addon.tf file, query the CCE add-on template information, and obtain the default configuration parameters of the add-on.

    variable "addon_template_name" {
      description = "CCE add-on template name"
      type        = string
      default     = "autoscaler"
    }
    
    variable "addon_version" {
      description = "CCE add-on template version"
      type        = string
    }
    
    # Obtain information about the CCE add-on template that meets the specified conditions in the specified region (if the region parameter is not specified, the region specified in the provider block is used by default). The information is used to create add-on-related resources.
    data "huaweicloud_cce_addon_template" "test" {
      cluster_id = data.huaweicloud_cce_cluster.cluster.id
      name       = var.addon_template_name
      version    = var.addon_version
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    cluster_id

    CCE cluster ID, which can be obtained from the query result in step 1.

    name

    Name of the add-on to be installed. The default value is autoscaler.

    version

    Version of the add-on to be installed. You can query the available versions on the CCE console.

  4. Configure the add-on parameters.

    Create the addon_config.tf file, parse the default configuration of the add-on template, and add the cluster_id and tenant_id fields.

    # Local variable, which is used to parse and merge the configuration of the add-on template.
    locals {
      # Parse the custom configuration in the add-on template.
      original_custom = jsondecode(data.huaweicloud_cce_addon_template.test.spec).cluster.custom
    
      # Merge the custom configuration: add the cluster_id and tenant_id fields and retain other fields.
      merged_custom = jsonencode({
        # Compared with the template, the cluster_id and tenant_id fields need to be added to the custom configuration.
        cluster_id                         = data.huaweicloud_cce_cluster.cluster.id
        tenant_id                          = var.project_id != "" ? var.project_id : try(data.huaweicloud_identity_projects.test.projects[0].id, "")
        # Retain the values of other custom fields.
        annotations                        = local.original_custom.annotations
        coresTotal                         = local.original_custom.coresTotal
        expander                           = local.original_custom.expander
        ignoreDaemonSetsUtilization        = local.original_custom.ignoreDaemonSetsUtilization
        ignoreLocalVolumeNodeAffinity      = local.original_custom.ignoreLocalVolumeNodeAffinity
        initialNodeGroupBackoffDuration    = local.original_custom.initialNodeGroupBackoffDuration
        logLevel                           = local.original_custom.logLevel
        maxEmptyBulkDeleteFlag             = local.original_custom.maxEmptyBulkDeleteFlag
        maxNodeGroupBackoffDuration        = local.original_custom.maxNodeGroupBackoffDuration
        maxNodeGroupBinPackingDuration     = local.original_custom.maxNodeGroupBinPackingDuration
        maxNodeProvisionTime               = local.original_custom.maxNodeProvisionTime
        maxNodesTotal                      = local.original_custom.maxNodesTotal
        memoryTotal                        = local.original_custom.memoryTotal
        multiAZBalance                     = local.original_custom.multiAZBalance
        multiAZEnabled                     = local.original_custom.multiAZEnabled
        newEphemeralVolumesPodScaleUpDelay = local.original_custom.newEphemeralVolumesPodScaleUpDelay
        node_match_expressions             = local.original_custom.node_match_expressions
        podDisruptionBudget                = local.original_custom.podDisruptionBudget
        resetUnNeededWhenScaleUp           = local.original_custom.resetUnNeededWhenScaleUp
        scaleDownDelayAfterAdd             = local.original_custom.scaleDownDelayAfterAdd
        scaleDownDelayAfterDelete          = local.original_custom.scaleDownDelayAfterDelete
        scaleDownDelayAfterFailure         = local.original_custom.scaleDownDelayAfterFailure
        scaleDownEnabled                   = local.original_custom.scaleDownEnabled
        scaleDownUnneededTime              = local.original_custom.scaleDownUnneededTime
        scaleDownUtilizationThreshold      = local.original_custom.scaleDownUtilizationThreshold
        scaleUpCpuUtilizationThreshold     = local.original_custom.scaleUpCpuUtilizationThreshold
        scaleUpMemUtilizationThreshold     = local.original_custom.scaleUpMemUtilizationThreshold
        scaleUpUnscheduledPodEnabled       = local.original_custom.scaleUpUnscheduledPodEnabled
        scaleUpUtilizationEnabled          = local.original_custom.scaleUpUtilizationEnabled
        scanInterval                       = local.original_custom.scanInterval
        skipNodesWithCustomControllerPods  = local.original_custom.skipNodesWithCustomControllerPods
        tolerations                        = local.original_custom.tolerations
        unremovableNodeRecheckTimeout      = local.original_custom.unremovableNodeRecheckTimeout
      })
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    original_custom

    Original custom configuration parsed from the add-on template.

    merged_custom

    Merged custom configuration, which includes cluster_id and tenant_id in addition to the original configuration.

    The value of tenant_id is preferentially the value of the project_id variable. If the variable is not configured, the value is automatically obtained from the project query result.

  5. Create add-on installation resources.

    Create the addon.tf file to declare the CCE add-on installation resources.

    resource "huaweicloud_cce_addon" "test" {
      cluster_id    = data.huaweicloud_cce_cluster.cluster.id
      template_name = var.addon_template_name
      version       = var.addon_version
    
      values {
        basic_json  = jsonencode(jsondecode(data.huaweicloud_cce_addon_template.test.spec).parameters.custom)
        custom_json = local.merged_custom
        flavor_json = jsonencode(jsondecode(data.huaweicloud_cce_addon_template.test.spec).parameters.flavor1)
      }
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    cluster_id

    ID of the CCE cluster where the add-on is to be installed.

    template_name

    Name of the CCE add-on to be installed.

    version

    Version of the CCE add-on to be installed.

    basic_json

    Basic configuration of the add-on, which is obtained from the add-on template.

    custom_json

    Custom configuration of the add-on, which is the configuration obtained after the merging in step 4.

    flavor_json

    Add-on specifications, which are obtained from the add-on template.

  6. Configure resource parameters.

    Create the terraform.tfvars file and preset the input parameters required by the resources. The following is an example configuration. Modify the parameters based on service requirements.

    cluster_name  = "devops-turbo"
    addon_version = "1.25.200"
    region_name   = "cn-north-4"
    availability_zone   = "cn-north-7c"           # Replace it with the actual AZ.

    The involved parameters are described in the table below.

    Parameter

    Description

    cluster_name

    Name of the created cluster.

    addon_version

    Add-on version. You can query the available versions on the CCE console.

    region_name

    Region name.

    availability_zone

    AZ where the nodes are located. Replace it with the actual AZ of the site.

  7. (Optional) If you need to specify an endpoint, create the endpoints.tf file.
    provider "huaweicloud" {
      region     = "cn-north-**"
      insecure  = true  # Skip SSL certificate verification.
      endpoints = {
        cce = "https://cce.{region}.myhuaweicloud.com"
      }
    }

    Replace {region} with the actual region (for example, cn-north-7c).

Step 3: Initialize and Apply the Terraform Configuration

  1. Initialize the environment.
    terraform init
  2. View the execution plan and check whether the resource changes meet your expectation.
    terraform plan
  3. After confirming the plan, create resources. Terraform will ask for your confirmation. Enter yes to confirm the execution.
    terraform apply
  4. View the created resources.
    terraform show

Step 4: Clear Resources

This operation will uninstall the AutoScaler add-on from the CCE cluster. After the uninstallation, the cluster will lose the automatic node scaling capability. Ensure that the add-on is no longer needed before performing this operation.

If you no longer need the resources created by Terraform, run the following command to release them:

terraform destroy

Terraform will ask for your confirmation. Enter yes to confirm the execution.

Helpful Links