Creating a CCE Cluster Using Terraform
In scenarios where containerized services are rapidly iterated, O&M engineers need to frequently create CCE clusters to deploy and manage containerized applications. To manually create a cluster on the console, engineers need to configure multiple parameters, such as the VPC, subnet, and cluster parameters. The process is complex and error-prone, especially when clusters need to be repeatedly created in multiple environments (development, testing, and production environments).
Terraform allows you to define the cluster creation process as code, enabling one-click, automated creation of CCE clusters and their dependent network resources. It supports version management and repeatable execution, making it suitable for the following scenarios:
- Automated creation and destruction of test clusters in CI/CD pipelines
- Batch creation of CCE clusters with consistent configurations in multiple environments
- Standardized cluster creation process to reduce manual operation differences
Prerequisites
- A server that can connect to the public network is available.
- You have obtained the AK/SK of your Huawei Cloud account, which has the permissions to create and modify CCE clusters. For details, see Access Keys.
- Terraform has been installed. For details, see Installing Terraform and Configuring the Huawei Cloud Provider.
Procedure
Step 1: Create a Project Directory and Initialize the Configuration
- Create a CCE cluster management directory (the directory name is user-defined).
mkdir terraform_cce_cluster cd ./terraform_cce_cluster
- 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.
- Query the AZs.
Create the az.tf file to query information about all AZs in a region. The information will be used to create resources for the CCE Turbo cluster.
# Obtain information about all AZs in a specified region (if the region parameter is not specified, the region specified in the provider block is used by default). The information will be used to create resources for the CCE Turbo cluster. data "huaweicloud_availability_zones" "test" {} - (Optional) If you need to specify an endpoint, create the endpoints.tf file.
provider "huaweicloud" { insecure = true # Skip SSL certificate verification. endpoints = { ecs = "https://ecs.{region}.myhuaweicloud.com" cce = "https://cce.{region}.myhuaweicloud.com" vpc = "https://vpc.{region}.myhuaweicloud.com" } }Replace {region} with the actual region (for example, cn-north-7c).
- (Optional) Create a VPC.
If there is already a VPC, skip this step and use the vpc_id variable to specify the existing VPC in subsequent steps. Create the vpc.tf file as follows:
variable "vpc_id" { description = "VPC ID" type = string default = "" } variable "vpc_name" { description = "VPC name" type = string default = "" validation { condition = var.vpc_id != "" || var.vpc_name != "" error_message = "If vpc_id is not specified, vpc_name must be specified." } } variable "vpc_cidr" { description = "VPC CIDR block" type = string default = "192.168.0.0/16" } # Create a VPC in the specified region (if the region parameter is not specified, the region specified in the provider block is used by default). This VPC is used to provide a network environment for the Turbo cluster. resource "huaweicloud_vpc" "test" { count = var.vpc_id == "" && var.subnet_id == "" ? 1 : 0 name = var.vpc_name cidr = var.vpc_cidr }The involved parameters are described in the table below.
Parameter
Description
count
Number of resources to be created. If vpc_id is specified, the existing VPC will be used, and no new VPC will be created.
name
VPC name
cidr
VPC CIDR block. The default CIDR block is 192.168.0.0/16.
- Create a VPC subnet.
If you have created a VPC in step 2, perform this step. If a subnet already exists, skip this step and use the subnet_id variable to specify the existing subnet. Create the vpc_subnet.tf file as follows:
variable "subnet_id" { description = "Subnet ID" type = string default = "" } variable "subnet_name" { description = "Subnet name" type = string default = "" validation { condition = var.subnet_id == "" || var.subnet_name == "" error_message = "If subnet_id is not specified, subnet_name must be specified." } } variable "subnet_cidr" { description = "Subnet CIDR block" type = string default = "" } variable "subnet_gateway_ip" { description = "Gateway IP address of the subnet" type = string default = "" } variable "availability_zone" { description = "AZ where the CCE cluster is to be created" type = string default = "" } # Create a VPC subnet in the specified region (if the region parameter is not specified, the region specified in the provider block is used by default). This subnet is used to provide a network environment for the Turbo cluster. resource "huaweicloud_vpc_subnet" "test" { count = var.subnet_id == "" ? 1 : 0 vpc_id = var.vpc_id != "" ? var.vpc_id : huaweicloud_vpc.test[0].id name = var.subnet_name cidr = var.subnet_cidr != "" ? var.subnet_cidr : cidrsubnet(huaweicloud_vpc.test[0].cidr, 4, 0) gateway_ip = var.subnet_cidr != "" ? cidrhost(var.subnet_cidr, 1) : cidrhost(cidrsubnet(huaweicloud_vpc.test[0].cidr, 4, 0), 1) availability_zone = var.availability_zone != "" ? var.availability_zone : try(data.huaweicloud_availability_zones.test.names[0], null) }The involved parameters are described in the table below.
Parameter
Description
count
Number of resources to be created. If subnet_id is specified, the existing subnet will be used, and no new subnet will be created.
vpc_id
ID of the VPC where the subnet needs to be created
name
Subnet name
cidr
Subnet CIDR block. If this parameter is not specified, a subnet is automatically created from the VPC CIDR block.
gateway_ip
Gateway IP address of the subnet. If this parameter is not specified, the first IP address in the subnet CIDR block is used.
availability_zone
AZ of the subnet. If this parameter is not specified, the first AZ found is used.
- Create a network interface subnet.
CCE Turbo clusters use Cloud Native Network 2.0. You need to create a network interface subnet to provide a network environment for containers. If there is already a network interface subnet, you can use the eni_ipv4_subnet_id variable to specify the existing network interface subnet.
Create the eni_subnet.tf file as follows:
variable "eni_ipv4_subnet_id" { description = "Network interface subnet ID" type = string default = "" } variable "eni_subnet_name" { description = "Name of the network interface subnet" type = string default = "" validation { condition = var.eni_ipv4_subnet_id == "" || var.eni_subnet_name == "" error_message = "If eni_ipv4_subnet_id is not specified, eni_subnet_name must be specified." } } variable "eni_subnet_cidr" { description = "CIDR block of the network interface subnet" type = string default = "" } # Create a network interface subnet in the specified region (if the region parameter is not specified, the region specified in the provider block is used by default). This subnet is used to provide a network environment for the Turbo cluster. resource "huaweicloud_vpc_subnet" "eni" { count = var.eni_ipv4_subnet_id == "" ? 1 : 0 vpc_id = var.vpc_id != "" ? var.vpc_id : huaweicloud_vpc.test[0].id name = var.eni_subnet_name cidr = var.eni_subnet_cidr != "" ? var.eni_subnet_cidr : cidrsubnet(huaweicloud_vpc.test[0].cidr, 4, 1) gateway_ip = var.eni_subnet_cidr != "" ? cidrhost(var.eni_subnet_cidr, 1) : cidrhost(cidrsubnet(huaweicloud_vpc.test[0].cidr, 4, 1), 1) availability_zone = var.availability_zone != "" ? var.availability_zone : try(data.huaweicloud_availability_zones.test.names[0], null) }The involved parameters are described in the table below.
Parameter
Description
count
Number of resources to be created. If eni_ipv4_subnet_id is specified, the existing subnet will be used, and no new subnet will be created.
vpc_id
ID of the VPC where the subnet needs to be created
name
Name of the network interface subnet
cidr
CIDR block of the network interface subnet. If this parameter is not specified, a subnet is automatically created from the VPC CIDR block.
gateway_ip
Gateway IP address of the subnet. If this parameter is not specified, the first IP address in the subnet CIDR block is used.
availability_zone
AZ of the subnet. If this parameter is not specified, the first AZ found is used.
The network interface subnet and the common VPC subnet are subnets in the same VPC, and their CIDR blocks cannot overlap.
- Create the cce_cluster.tf file to create a CCE cluster.
variable "cluster_name" { description = "CCE cluster name" type = string default = "" } variable "cluster_flavor_id" { description = "CCE cluster flavor ID" type = string default = "cce.s1.small" } variable "cluster_version" { description = "CCE cluster version" type = string default = null nullable = true } variable "cluster_type" { description = "CCE cluster type" type = string default = "VirtualMachine" } variable "container_network_type" { description = "Container network model" type = string default = "eni" } variable "cluster_description" { description = "CCE cluster description" type = string default = "" } variable "cluster_tags" { description = "CCE cluster tags" type = map(string) default = {} } # Create a CCE cluster in the specified region (if the region parameter is not specified, the region specified in the provider block is used by default). This CCE cluster is used to deploy and manage high-performance containerized applications. resource "huaweicloud_cce_cluster" "test" { name = var.cluster_name flavor_id = var.cluster_flavor_id cluster_version = var.cluster_version cluster_type = var.cluster_type container_network_type = var.container_network_type vpc_id = var.vpc_id != "" ? var.vpc_id : huaweicloud_vpc.test[0].id subnet_id = var.subnet_id != "" ? var.subnet_id : huaweicloud_vpc_subnet.test[0].id eni_subnet_id = var.eni_ipv4_subnet_id != "" ? var.eni_ipv4_subnet_id : huaweicloud_vpc_subnet.eni[0].ipv4_subnet_id description = var.cluster_description tags = var.cluster_tags }The involved parameters are described in the table below.
Parameter
Description
name
Cluster name
flavor_id
Cluster flavor ID. The default value is cce.s1.small.
cluster_version
Cluster version. If this parameter is set to null, the default version is used.
cluster_type
Master node architecture. The value can be VirtualMachine (x86) or ARM64 (Kunpeng).
container_network_type
Container network model. The value can be overlay_l2 (container tunnel network, used for creating standard CCE clusters), vpc-router (VPC network, used for creating standard CCE clusters), or eni (Cloud Native Network 2.0, used for creating CCE Turbo clusters).
vpc_id
ID of the VPC used by the cluster
subnet_id
ID of the subnet used by cluster nodes
eni_subnet_id
ID of the network interface subnet (IPv4 subnet) used by containers
description
Cluster description
tags
Cluster tags
The container_network_type parameter determines the type of the cluster to be created and the required network resources. If you select eni, you need to create a network interface subnet. If you select overlay_l2 or vpc-router, you do not need to create a network interface subnet.
- Configure resource parameters. The following is an example configuration. Modify the parameters based on service requirements.
Create a terraform.tfvars file and preset the input parameters required by the resources.
vpc_name = "tf_test_vpc" subnet_name = "tf_test_subnet" eni_subnet_name = "tf_test_eni_subnet" cluster_name = "tf-test-cluster" cluster_description = "Created by terraform script" availability_zone = "cn-north-7c" # Replace it with the actual AZ. cluster_tags = { owner = "terraform" }
Step 3: Initialize and Apply the Terraform Configuration
- Initialize the environment.
terraform init
- View the execution plan and check whether the resource changes meet your expectation.
terraform plan
- After confirming the plan, create resources. Terraform will ask for your confirmation. Enter yes to confirm the execution.
terraform apply
- View the created resources.
terraform show
Step 4: Clear Resources
This operation will permanently delete all resources (including the VPC, subnets, and CCE cluster) created by Terraform. Ensure that there is no important service data in the resources 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
- Installing Terraform and Configuring the Huawei Cloud Provider: how to install Terraform and configure the Huawei Cloud provider
- Suggestions on CCE Cluster Selection: CCE cluster specifications and selection suggestions
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot