Terraform is an open source tool that allows you to preconfigure and manage your cloud infrastructure in a secure and efficient manner. You can use Terraform to manage Alibaba Cloud resources.
Prerequisites
Terraform is installed.
NoteYou must install Terraform 0.12.28 or later. You can run the terraform --version command to query the Terraform version.
By default, Cloud Shell has preinstalled Terraform and configured your account information. You do not need to modify the configurations.
For more information about how to install Terraform by using a method other than Cloud Shell, see Install and configure Terraform in the local PC.
Your account information is configured.
Run the following commands to create environment variables to store identity authentication information.
Linux environment
export ALICLOUD_ACCESS_KEY="************" # Replace the value with the AccessKey ID of your Alibaba Cloud account. export ALICLOUD_SECRET_KEY="************" # Replace the value with the AccessKey secret of your Alibaba Cloud account. export ALICLOUD_REGION="cn-beijing" # Replace the value with the ID of the region in which your cluster resides.
Windows environment
set ALICLOUD_ACCESS_KEY="************" # Replace the value with the AccessKey ID of your Alibaba Cloud account. set ALICLOUD_SECRET_KEY="************" # Replace the value with the AccessKey secret of your Alibaba Cloud account. set ALICLOUD_REGION="cn-beijing" # Replace the value with the ID of the region in which your cluster resides.
NoteTo improve the flexibility and security of permission management, we recommend that you create a Resource Access Management (RAM) user named Terraform. Then, create an AccessKey pair for the RAM user and grant permissions to the RAM user. For more information, see Create a RAM user and Grant permissions to RAM users.
Use Terraform to create an ACK Serverless cluster
Create a working directory and a file named main.tf in the directory.
The main.tf file contains the following Terraform configuration:
Create a virtual private cloud (VPC) and create a vSwitch in the VPC.
Create an ACK Serverless cluster.
provider "alicloud" { } variable "k8s_name_prefix" { description = "The name prefix used to create ASK cluster." default = "ask-example" } # The 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" } 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.1 } resource "alicloud_cs_serverless_kubernetes" "serverless" { name = local.k8s_name_ask version = "1.28.3-aliyun.1" # Replace the value with the version of the cluster that you want to create. cluster_spec = "ack.pro.small" 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 # Enable the RAM Roles for Service Accounts (RRSA) feature to configure service accounts. enable_rrsa = true load_balancer_spec = "slb.s2.small" time_zone = "Asia/Shanghai" service_cidr = "10.13.0.0/16" service_discovery_types = ["CoreDNS"] # tags tags = { "cluster" = "ack-serverless" } # addons addons { name = "nginx-ingress-controller" # Expose the cluster to the Internet config = "{\"IngressSlbNetworkType\":\"internet\",\"IngressSlbSpec\":\"slb.s2.small\"}" # To expose the cluster to the Internet, specify the following configuration: # config = "{\"IngressSlbNetworkType\":\"intranet\",\"IngressSlbSpec\":\"slb.s2.small\"}" } addons { name = "metrics-server" } addons { name = "knative" } addons { name = "managed-arms-prometheus" } addons { name = "logtail-ds" # Specify the name of a Simple Log Service project. # config = "{\"sls_project_name\":\"<YOUR-SLS-PROJECT-NAME>}\"}" } }
Run the following command to initialize the Terraform runtime environment:
terraform init
If the following information is returned, Terraform is initialized:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.184.0... ... 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.
Run the following command to create an execution plan:
terraform plan
If the following information is returned, the execution plan is created:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 3 to add, 0 to change, 0 to destroy. ...
Run the following command to create a cluster:
terraform apply
When the following information is returned, input
yes
and press Enter. Then, the cluster is created.... 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_serverless_kubernetes.serverless: Creation complete after 8m26s [id=************] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Use Terraform to delete an ACK Serverless cluster
You can run the following command to delete an ACK Serverless cluster that is created by using Terraform:
terraform destroy
When the following information is returned, input yes
and press Enter. Then, the cluster is deleted.
...
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
...
Destroy complete! Resources: 3 destroyed.