SecretId and SecretKey. If you already have them, skip this step.SecretId/SecretKey.export TENCENTCLOUD_SECRET_ID="xxx" # Replace it with the `SecretId` of the access keyexport TENCENTCLOUD_SECRET_KEY="xxx" # Replace it with the `SecretKey` of the access key
provider block of the Terraform configuration fileprovider.tf file under the user directory and enter the following content:provider "tencentcloud" {secret_id = "xxx" # Replace it with the `SecretId` of the access keysecret_key = "xxx" # Replace it with the SecretKey` of the access key}
main.tf under it.main.tf file describes the following Terraform configurations:main.tf file is as follows:# Identify the use of Tencent Cloud Terraform Providerterraform {required_providers {tencentcloud = {source = "tencentcloudstack/tencentcloud"}}}# Define local variables and modify the values as needed when using them in subsequent code blocks.locals {region = "xxx" # Region, such as `ap-beijing`, i.e. Beijingzone1 = "xxx" # An AZ in the region, such as `ap-beijing-1`, i.e. Beijing Zone 1vpc_name = "xxx" # Set the VPC name, such as `tke-tf-demo`vpc_cidr_block = "xxx" # CIDR block of the VPC, such as `10.0.0.0/16`subnet1_name = "xxx" # Name of subnet 1, such as `tke-tf-demo-sub1`subnet1_cidr_block = "xxx" # CIDR block of subnet 1, such as `10.0.1.0/24`cluster_name = "xxx" # TKE cluster name, such as `tke-tf-demo-cluster`network_type = "xxx" # Network mode of the managed TKE cluster, such as `GR`, which indicates Global Routecluster_cidr = "xxx" # Container network of the cluster, such as `172.26.0.0/20`. It cannot conflict with the VPC CIDR and other cluster CIDRs in the same VPC.cluster_version = "xxx" # Kubernetes version of the TKE cluster, such as `1.22.5`}# Basic configuration of the Tencent Cloud `provider`provider "tencentcloud" {# Enter the `SecretId` and `SecretKey` if you use the configuration file. It is recommended to inject the key with environment variables.# secret_id = "xxx"# secret_key = "xxx"region = local.region}# Declare VPC resourcesresource "tencentcloud_vpc" "vpc_example" {name = local.vpc_namecidr_block = local.vpc_cidr_block}# Declare subnet resourcesresource "tencentcloud_subnet" "subnet_example" {availability_zone = local.zone1cidr_block = local.subnet1_cidr_blockname = local.subnet1_namevpc_id = tencentcloud_vpc.vpc_example.id # The VPC ID of the specified subnet resource is the ID of the above VPC.}# Declare TKE cluster resources and create a cluster with the network set as Global Routeresource "tencentcloud_kubernetes_cluster" "managed_cluster_example" {vpc_id = tencentcloud_vpc.vpc_example.id # Reference the VPC ID created abovecluster_name = local.cluster_namenetwork_type = local.network_typecluster_cidr = local.cluster_cidrcluster_version = local.cluster_version}# You can use the following declaration to create a cluster in VPC-CNI mode.# resource "tencentcloud_kubernetes_cluster" "managed_cluster_example" {# vpc_id = tencentcloud_vpc.vpc_example.id # Reference the VPC ID created above# cluster_name = local.cluster_name# network_type = "VPC-CNI"# eni_subnet_ids = [tencentcloud_subnet.subnet_example.id]# service_cidr = "172.16.0.0/24"# cluster_version = local.cluster_version# }
cam.tf file with the following content under the working directory.########################### Please add declaration configuration in the Terraform configuration file as needed. You do not need to add it for roles that have obtained permissions in the console. ############## Create the preset role `TKE_QCSRole` for the serviceresource "tencentcloud_cam_role" "TKE_QCSRole" {name = "TKE_QCSRole"document = <<EOF{"statement": [{"action":"name/sts:AssumeRole","effect":"allow","principal":{"service":"ccs.qcloud.com"}}],"version":"2.0"}EOFdescription = "The current role is the Tencent Cloud TKE service role, and it will access your other Tencent Cloud resources within the permissions granted by the associated policies."}# Preset policy `QcloudAccessForTKERole`data "tencentcloud_cam_policies" "qca" {name = "QcloudAccessForTKERole"}# Preset policy `QcloudAccessForTKERoleInOpsManagement`data "tencentcloud_cam_policies" "ops_mgr" {name = "QcloudAccessForTKERoleInOpsManagement"}# Associate the policy `QcloudAccessForTKERole` with the role `TKE_QCSRole`resource "tencentcloud_cam_role_policy_attachment" "QCS_QCA" {role_id = lookup(tencentcloud_cam_role.TKE_QCSRole, "id")policy_id = data.tencentcloud_cam_policies.qca.policy_list.0.policy_id}# Associate the policy `QcloudAccessForTKERoleInOpsManagement` with the role `TKE_QCSRole`resource "tencentcloud_cam_role_policy_attachment" "QCS_OpsMgr" {role_id = lookup(tencentcloud_cam_role.TKE_QCSRole, "id")policy_id = data.tencentcloud_cam_policies.ops_mgr.policy_list.0.policy_id}########################### Create the role `TKE_QCSRole` and grant permissions to it with the above declaration ###################################################### Create the role `IPAMDofTKE_QCSRole` and grant permissions to it with the below declaration ############################ Create the preset role `IPAMDofTKE_QCSRole` for the serviceresource "tencentcloud_cam_role" "IPAMDofTKE_QCSRole" {name = "IPAMDofTKE_QCSRole"document = <<EOF{"statement": [{"action":"name/sts:AssumeRole","effect":"allow","principal":{"service":"ccs.qcloud.com"}}],"version":"2.0"}EOFdescription = "The current role is the IPAMD service role, and it will access your other Tencent Cloud resources within the permissions granted by the associated policies."}# Preset policy `QcloudAccessForIPAMDofTKERole`data "tencentcloud_cam_policies" "qcs_ipamd" {name = "QcloudAccessForIPAMDofTKERole"}# Associate the policy `QcloudAccessForIPAMDofTKERole` with the role `IPAMDofTKE_QCSRole`resource "tencentcloud_cam_role_policy_attachment" "QCS_Ipamd" {role_id = lookup(tencentcloud_cam_role.IPAMDofTKE_QCSRole, "id")policy_id = data.tencentcloud_cam_policies.qcs_ipamd.policy_list.0.policy_id}########################### Create the role `IPAMDofTKE_QCSRole` and grant permissions to it with the above declaration ###################################################### Create the role `TKE_QCSLinkedRoleInEKSLog` and grant permissions to it with the below declaration ############################ To enable log collection for super nodes, create the preset role `TKE_QCSLinkedRoleInEKSLog` for the service.resource "tencentcloud_cam_service_linked_role" "service_linked_role" {qcs_service_name = ["cvm.qcloud.com", "ekslog.tke.cloud.tencent.com"]description = "tke log role created by terraform"tags = {"createdBy" = "terraform"}}
terraform init
Initializing the backend...Initializing provider plugins...- Finding tencentcloudstack/tencentcloud versions matching "~> 1.78.13"...- Installing tencentcloudstack/tencentcloud v1.78.13......You may now begin working with Terraform. Try running "terraform plan" to seeany changes that are required for your infrastructure. All Terraform commandsshould now work....
terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:+ createTerraform will perform the following actions:...Plan: 3 to add, 0 to change, 0 to destroy....
terraform apply
...Plan: 3 to add, 0 to change, 0 to destroy.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 as prompted to create the resource. The following information is returned:...Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
nodepool.tf.
The content of the nodepool.tf file is as follows:# Define local variables and modify the values as needed when using them in subsequent code blocks.# You can also reference Terraform related resource instance (such as `tencentcloud_kubernetes_cluster`) to obtain the desired values.locals {node_pool_name = "xxx" # Node pool name, such as `tke-tf-demo-node-pool`max_node_size = xxx # Max number of nodes in the node poolmin_node_size = xxx # Min number of nodes in the node poolcvm_instance_type = "xxx" # CVM instance in the node pool. For valid values, see https://cloud.tencent.com/document/api/213/15749cvm_pass_word = "xxx" # Login password for the CVM instance in the node pool. Password length: 8-16 characters.security_group_ids = ["sg-xxx", "sg-xxx"] # Array of IDs of security groups associated with the node pool}# Declare TKE node pool resourcesresource "tencentcloud_kubernetes_node_pool" "example_node_pool" {cluster_id = tencentcloud_kubernetes_cluster.managed_cluster_example.id # Associate the node pool with the cluster created abovedelete_keep_instance = false # Set it to `false`, which indicates the associated CVM instance is deleted when you delete the node pool.max_size = local.max_node_sizemin_size = local.min_node_sizename = local.node_pool_namevpc_id = tencentcloud_vpc.vpc_example.idsubnet_ids = [tencentcloud_subnet.subnet_example.id] # Array of IDs of subnets associated with the node poolauto_scaling_config {instance_type = local.cvm_instance_type# key_ids = ["xxx"] # Set the login key for the CVM instance in the node poolpassword = local.cvm_pass_word # Set the login password for the CVM instance in the node poolsecurity_group_ids = local.security_group_ids}}
terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:+ createTerraform will perform the following actions:...Plan: 1 to add, 0 to change, 0 to destroy....
terraform apply
...Plan: 1 to add, 0 to change, 0 to destroy.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 as prompted to create the resource. The following information is returned:...Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform destroy
...Plan: 0 to add, 0 to change, 3 to destroy.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 as prompted to confirm the deletion. The following information is returned:...Destroy complete! Resources: 3 destroyed.
Feedback