Use Terraform to create an ACK Serverless cluster
This topic describes how to create an ACK serverless cluster using Terraform, including VPC, vSwitch, and security group setup.
Run the complete example in one click from Terraform Explorer.
Prerequisites
Before you begin, ensure that you have:
ACK activated with service roles assigned
An AccessKey pair for a RAM user
The minimum RAM policy attached to the RAM user
A Terraform runtime environment (version 0.12.28 or later):
Terraform Explorer -- browser-based, no installation required (Recommended)
Cloud Shell -- preinstalled with your credentials
Local installation -- for custom or offline environments
Verify your Terraform version with terraform --version. This guide requires version 0.12.28 or later.
Use a RAM user instead of your Alibaba Cloud account to reduce security risks from compromised credentials.
Minimum RAM policy
Attach the following policy to the RAM user. It grants the minimum permissions to create and delete VPCs, vSwitches, security groups, and ACK clusters.
{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": [
"vpc:CreateVpc",
"vpc:CreateVSwitch",
"vpc:DescribeVpcAttribute",
"vpc:DescribeRouteTableList",
"vpc:DescribeVSwitchAttributes",
"ecs:CreateSecurityGroup",
"ecs:ModifySecurityGroupPolicy",
"ecs:DescribeSecurityGroups",
"ecs:DescribeSecurityGroupAttribute",
"ecs:ListTagResources",
"cs:CreateCluster",
"cs:DescribeTaskInfo",
"cs:DescribeClusterDetail",
"vpc:DeleteVpc",
"vpc:DeleteVSwitch",
"cs:DeleteCluster",
"ecs:DeleteSecurityGroup"
],
"Resource": "*"
}
]
}Resources
Some resources in this configuration incur charges. Release them when no longer needed.
alicloud_eci_zones: Queries the zones where ECI instances are available.
alicloud_vpc: Creates a virtual private cloud (VPC).
alicloud_vswitch: Creates a virtual switch (vSwitch) to divide a VPC into one or more subnets.
alicloud_security_group: Creates a security group.
alicloud_cs_serverless_kubernetes: Creates an ACK serverless cluster.
Create the cluster
Step 1: Write the Terraform configuration
Create a working directory and add a main.tf file with the following configuration:
provider "alicloud" {
region = var.region_id
}
variable "region_id" {
type = string
default = "cn-shenzhen"
}
variable "cluster_spec" {
type = string
description = "Cluster specification. Valid values: ack.standard (Standard managed), ack.pro.small (Professional managed)."
default = "ack.pro.small"
}
variable "k8s_name_prefix" {
description = "Name prefix for the ACK serverless cluster."
default = "ask-example"
}
variable "ack_version" {
type = string
description = "Kubernetes version."
default = "1.32.7-aliyun.1"
}
# Default resource names.
locals {
k8s_name_ask = substr(join("-", [var.k8s_name_prefix, "ask"]), 0, 63)
new_vpc_name = "tf-vpc-172-16"
new_vsw_name = "tf-vswitch-172-16-0"
new_sg_name = "tf-sg-172-16"
}
data "alicloud_eci_zones" "default" {}
resource "alicloud_vpc" "vpc" {
vpc_name = local.new_vpc_name
cidr_block = "172.16.0.0/12"
}
resource "alicloud_vswitch" "vsw" {
vswitch_name = local.new_vsw_name
vpc_id = alicloud_vpc.vpc.id
cidr_block = cidrsubnet(alicloud_vpc.vpc.cidr_block, 8, 8)
zone_id = data.alicloud_eci_zones.default.zones.0.zone_ids.0
}
resource "alicloud_security_group" "group" {
security_group_name = local.new_sg_name
vpc_id = alicloud_vpc.vpc.id
}
resource "alicloud_cs_serverless_kubernetes" "serverless" {
name = local.k8s_name_ask
version = var.ack_version
cluster_spec = var.cluster_spec
vpc_id = alicloud_vpc.vpc.id
vswitch_ids = split(",", join(",", alicloud_vswitch.vsw.*.id))
new_nat_gateway = true
endpoint_public_access_enabled = true
deletion_protection = false
security_group_id = alicloud_security_group.group.id
enable_rrsa = true
time_zone = "Asia/Shanghai"
service_cidr = "10.13.0.0/16"
service_discovery_types = ["CoreDNS"]
tags = {
"cluster" = "ack-serverless"
}
# Nginx Ingress Controller with internet-facing SLB
addons {
name = "nginx-ingress-controller"
config = "{\"IngressSlbNetworkType\":\"internet\",\"IngressSlbSpec\":\"slb.s2.small\"}"
# For internal SLB, use:
# config = "{\"IngressSlbNetworkType\":\"intranet\",\"IngressSlbSpec\":\"slb.s2.small\"}"
}
addons {
name = "metrics-server"
}
addons {
name = "knative"
}
addons {
name = "managed-arms-prometheus"
}
addons {
name = "logtail-ds"
# To specify a Simple Log Service (SLS) project:
# config = "{\"sls_project_name\":\"<your-sls-project-name>\"}"
}
}Cluster parameters
| Parameter | Description | Default |
|---|---|---|
region_id | Region for resource deployment | cn-shenzhen |
cluster_spec | Cluster edition. ack.standard for Standard, ack.pro.small for Professional | ack.pro.small |
ack_version | Kubernetes version | 1.32.7-aliyun.1 |
new_nat_gateway | Create a NAT Gateway for internet access | true |
endpoint_public_access_enabled | Expose the API server endpoint publicly | true |
enable_rrsa | Enable RAM Roles for Service Accounts (RRSA) | true |
service_cidr | CIDR block for Kubernetes Services | 10.13.0.0/16 |
service_discovery_types | Service discovery mechanism | ["CoreDNS"] |
Addons
| Addon | Purpose |
|---|---|
nginx-ingress-controller | Ingress traffic routing through an internet-facing Server Load Balancer (SLB) instance (spec: slb.s2.small) |
metrics-server | Pod resource metrics for Horizontal Pod Autoscaler |
knative | Serverless workload framework |
managed-arms-prometheus | Managed Prometheus monitoring |
logtail-ds | Log collection agent for Simple Log Service (SLS) |
Step 2: Initialize Terraform
terraform initExpected output:
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.Step 3: Preview the execution plan
terraform planReview the output to confirm that 4 resources will be created:
Plan: 4 to add, 0 to change, 0 to destroy.Step 4: Apply the configuration
terraform applyWhen prompted, type yes and press Enter. Cluster creation typically takes 8--10 minutes.
Expected output upon completion:
alicloud_cs_serverless_kubernetes.serverless: Creation complete after 8m26s [id=************]
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.Verify the result
Use either method to confirm that the cluster was created:
Terraform CLI: Run
terraform showto display the resource details.ACK console: Log in to the Container Service for Kubernetes console and check the cluster list.
Clean up resources
To release all resources created in this guide, run:
terraform destroyWhen prompted, type yes to confirm. For more information, see Common Terraform commands.