All Products
Search
Document Center

Container Compute Service:Create an ACS cluster using Terraform

Last Updated:Jun 02, 2026

Create an ACS cluster by defining infrastructure as code with Terraform.

Prerequisites

  • You have activated Container Compute Service ACS.

  • Your Alibaba Cloud account must have full permissions on all resources. If the credentials of your Alibaba Cloud account are leaked, you may face significant security risks. We recommend that you use a Resource Access Management (RAM) user and create an AccessKey for the RAM user. For more information, see Create a RAM user and Create an AccessKey.

  • Attach the following least privilege policy to the RAM user that you use to run Terraform commands. This policy grants the RAM user permissions to manage the resources in this example. For more information, see Manage RAM user permissions.

    This policy grants the RAM user permissions to create, view, and delete ACS clusters and dependent cloud resources.

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "cs:CreateCluster",
            "cs:DescribeTaskInfo",
            "cs:DescribeClusterDetail",
            "cs:GetClusterCerts",
            "cs:CheckControlPlaneLogEnable",
            "cs:DeleteCluster",
            "cs:GetClusterAuditProject",
            "cs:ModifyCluster",
            "vpc:DescribeNatGateways"
          ],
          "Resource": "*"
        }
      ]
    }
  • Prepare a Terraform runtime environment using one of the following methods.

    Important

    Terraform 0.12.28 or later is required. Run terraform --version to verify.

Resources used

Note

Release these resources when no longer needed to avoid unexpected charges.

Create an ACS cluster using Terraform

The following example creates an ACS cluster with default addons: alibaba-log-controller (logging), arms-prometheus (monitoring), managed-coredns (DNS), and managed-metrics-server (metrics).

  1. Create a working directory with a file named main.tf . Copy the following code into main.tf and modify as needed.

provider "alicloud" {
  region      = var.region_id
}
variable "region_id" {
  type        = string
  default     = "cn-guangzhou"
}
variable "availability_zone" {
  type        = list(string)
  description = "The availability zones of the vSwitches."
  default     = ["cn-guangzhou-a", "cn-guangzhou-b"]
}
variable "k8s_name_prefix" {
  description = "The name prefix used to create the ACS cluster."
  default     = "acs-example"
}
variable "acs_version" {
  type        = string
  description = "The desired Kubernetes version."
  default     = "1.31.1-aliyun.1"      # Modify the value as needed.
}
variable "service_cidr" {
  type        = string
  description = "The CIDR block for Kubernetes services."
  default     = "10.13.0.0/16"      # Modify the value as needed.
}

# Specifies the addons to install in the ACS cluster, including alibaba-log-controller (logging), arms-prometheus (monitoring), managed-coredns, and managed-metrics-server.
variable "cluster_addons" {   # Modify the value as needed.
  type = list(object({
    name   = string
    config = string
  }))
  default = [
    {
      "name"   = "arms-prometheus",
      "config" = "",
    },
    {
      "name"   = "alibaba-log-controller",
      "config" = "",
    },
    {
      "name"   = "managed-coredns",
      "config" = "",
    },
    {
      "name"   = "managed-metrics-server",
      "config" = "",
    }
  ]
}
# Default resource names.
locals {
  k8s_name_acs     = substr(join("-", [var.k8s_name_prefix, "acs"]), 0, 63)
  log_project_name = "log-for-${local.k8s_name_acs}"
}

# Create the ACS cluster.
resource "alicloud_cs_managed_kubernetes" "default" {
  name                         = local.k8s_name_acs   # The name of the Kubernetes cluster.
  cluster_spec                 = "ack.pro.small"
  profile                      = "Acs"
  version                      = var.acs_version
  zone_ids                     = var.availability_zone
  is_enterprise_security_group = true
  control_plane_log_ttl        = "30"    
  new_nat_gateway              = true   # Specifies whether to create a new NAT gateway when creating the Kubernetes cluster. Default value: true.                                                           
  control_plane_log_components = ["apiserver", "kcm", "scheduler", "ccm"] # The control plane components whose logs you want to collect.
  deletion_protection          = true
  # Configure the ServiceAccount using RRSA.
  enable_rrsa                  = true
  timezone                     = "Asia/Shanghai"
  service_cidr                 = var.service_cidr  # Modify the value as needed.
  slb_internet_enabled         = false  # Specifies whether to create an internet-facing load balancer for the API server. Default value: false.
  dynamic "addons" {  # Addon management.
    for_each = var.cluster_addons
    content {
      name   = lookup(addons.value, "name", var.cluster_addons)
      config = lookup(addons.value, "config", var.cluster_addons)
    }
  }
}
Note

This configuration is for reference only. Modify as needed.

  1. Initialize the Terraform runtime environment.

terraform init

Expected output on success:

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
  1. Create an execution plan and preview the changes.

terraform plan
  1. Create the cluster.

terraform apply

Enter yes when prompted and press Enter. Expected output on success:

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

...
alicloud_cs_managed_kubernetes.default: Creation complete after 5m48s [id=ccb53e72ec6c447c990762800********]
...

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  1. Verify the results

Terraform show

View the resources created by Terraform.

terraform show

ACS console

Log on to the Container Compute Service console to view the created cluster.

Clean up resources

Run terraform destroy to release resources you no longer need. The terraform destroy command is documented in Common Commands.

terraform destroy