Import command in Terraform to import a single resource in the format of terraform import [Resource type].[Name] [Input parameter]. The name is custom, and the input parameter is a required string for resource query (which is an ID in most cases and a name or multi-field combination for certain resources). Taking the CVM instance as an example, the import command indicated in Import is as follows:$ terraform import tencentcloud_instance.ins ins-jvu2hiw2 -allow-missing-config
-allow-missing-config indicates not to require a pre-declared block locally; otherwise, you need to pre-write a resource [Resource type].[Name] {} block in the file. After the import, fields will not be written into the .tf file, and you need to run terraform show to view the code of the imported resource:# tencentcloud_instance.ins:resource "tencentcloud_instance" "ins" {allocate_public_ip = trueavailability_zone = "ap-guangzhou-3"create_time = "2022-01-01T01:11:11Z"id = "ins-xxxxxxxx"image_id = "img-xxxxxxxx"instance_charge_type = "POSTPAID_BY_HOUR"instance_name = "xxxxxxxx"instance_status = "RUNNING"instance_type = "S3.MEDIUM2"internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"internet_max_bandwidth_out = 1key_name = "skey-xxxxxxxx"private_ip = "10.0.1.1"project_id = 0public_ip = "1.1.1.1"running_flag = truesecurity_groups = ["sg-xxxxxxxx",]subnet_id = "subnet-xxxxxxxx"system_disk_id = "disk-xxxxxxxx"system_disk_size = 50system_disk_type = "CLOUD_PREMIUM"tags = {}vpc_id = "vpc-xxxxxxxx"}
id, create_time, and public_ip as indicated in Attributes Reference) before the import.resource "tencentcloud_instance" "ins" {allocate_public_ip = trueavailability_zone = "ap-guangzhou-3"# create_time = "2022-01-01T01:11:11Z"# id = "ins-xxxxxxxx"image_id = "img-xxxxxxxx"instance_charge_type = "POSTPAID_BY_HOUR"instance_name = "xxxxxxxx"# instance_status = "RUNNING"instance_type = "S3.MEDIUM2"internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"internet_max_bandwidth_out = 1key_name = "skey-xxxxxxxx"private_ip = "10.0.1.1"project_id = 0# public_ip = "1.1.1.1"running_flag = truesecurity_groups = ["sg-xxxxxxxx",]subnet_id = "subnet-xxxxxxxx"system_disk_id = "disk-xxxxxxxx"system_disk_size = 50system_disk_type = "CLOUD_PREMIUM"tags = {}vpc_id = "vpc-xxxxxxxx"}
Import command and read-only fields of each resource here. If they are not found, the resource cannot be imported currently.$ brew install terraformer
terraformer import tencentcloud --resources="vpc,cvm" --regions=ap-guangzhou
./generated directory by default as shown below:.└── tencentcloud├── cvm│ └── ap-guangzhou│ ├── instance.tf│ ├── key_pair.tf│ ├── outputs.tf│ ├── provider.tf│ ├── terraform.tfstate│ └── variables.tf└── vpc└── ap-guangzhou├── outputs.tf├── provider.tf├── terraform.tfstate└── vpc.tf
source field to the generated provider.tf file, with the value of tencentcloudstack/tencentcloud.provider "tencentcloud" {version = "~> 1.77.11"}terraform {required_providers {tencentcloud = {source = "tencentcloudstack/tencentcloud" # Add `source` to specify the namespaceversion = "~> 1.77.11"}}}
.├── .terraform│ └── providers # Referenced providers├── .terraform.lock.hcl # Provider lock version├── main.tf # tf. file├── vars.tf # tf. file├── outputs.tf # tf. file└── terraform.tfstate # State file

terraform apply command and complete the deployment, the terraform.tfstate file will be generated. It is a JSON file stored locally by default or configured in a remote bucket (you need to configure the backend) to describe the mapping between resources declared by Terraform and real cloud resources. If terraform.tfstate does not exist in the local directory or backend, or no cloud resource data is written into it, Terraform will consider that no resources are deployed and run apply for resource creation.tfstate mapping is regarded as creation. Therefore, you can copy a file, modify the region, and run apply for cross-region replication of resources.eks-app-guangzhou├── crds.tf├── infra.tf├── main.tf├── terraform.log└── terraform.tfstate
main.tf specifies the meta information of Terraform and the provider as follows:terraform {required_providers {tencentcloud = {source = "tencentcloudstack/tencentcloud"}}}provider "tencentcloud" {region = "ap-guangzhou"}
infra.tf specifies the TKE serverless cluster and required resources: VPC, subnet, security group, TKE serverless cluster, and CLB instance as follows:# The IP address for the test on the external accessibility of the servicevariable "accept_ip" {description = "Use EnvVar: $TF_VAR_accept_ip instead"}resource "tencentcloud_vpc" "vpc" {name = "eks-vpc"cidr_block = "10.2.0.0/16"}resource "tencentcloud_subnet" "sub" {vpc_id = tencentcloud_vpc.vpc.idname = "eks-subnet"cidr_block = "10.2.0.0/20"availability_zone = "ap-guangzhou-3"}resource "tencentcloud_security_group" "sg" {name = "eks-sg"}resource "tencentcloud_security_group_lite_rule" "sgr" {security_group_id = tencentcloud_security_group.sg.idingress = ["ACCEPT#10.2.0.0/16#ALL#ALL","ACCEPT#${var.accept_ip}#ALL#ALL"]}resource "tencentcloud_eks_cluster" "foo" {cluster_name = "tf-test-eks"k8s_version = "1.20.6"vpc_id = tencentcloud_vpc.vpc.idsubnet_ids = [tencentcloud_subnet.sub.id,]cluster_desc = "test eks cluster created by terraform"service_subnet_id = tencentcloud_subnet.sub.idenable_vpc_core_dns = trueneed_delete_cbs = truepublic_lb {enabled = truesecurity_policies = [var.accept_ip]}internal_lb {enabled = truesubnet_id = tencentcloud_subnet.sub.id}}resource "tencentcloud_clb_instance" "ingress-lb" {address_ip_version = "ipv4"clb_name = "example-lb"internet_bandwidth_max_out = 1internet_charge_type = "BANDWIDTH_POSTPAID_BY_HOUR"load_balancer_pass_to_target = truenetwork_type = "OPEN"security_groups = [tencentcloud_security_group.sg.id]vpc_id = tencentcloud_vpc.vpc.id}
crds.tf specifies the CRD of the TKE Serverless cluster as follows:locals {kubeconfig = yamldecode(tencentcloud_eks_cluster.foo.kube_config)}provider "kubernetes" {host = local.kubeconfig.clusters[0].cluster.servercluster_ca_certificate = base64decode(local.kubeconfig.clusters[0].cluster["certificate-authority-data"])client_key = base64decode(local.kubeconfig.users[0].user["client-key-data"])client_certificate = base64decode(local.kubeconfig.users[0].user["client-certificate-data"])}resource "kubernetes_namespace" "test" {metadata {name = "nginx"}}resource "kubernetes_deployment" "test" {metadata {name = "nginx"namespace = kubernetes_namespace.test.metadata.0.name}spec {replicas = 2selector {match_labels = {app = "MyTestApp"}}template {metadata {labels = {app = "MyTestApp"}}spec {container {image = "nginx"name = "nginx-container"port {container_port = 80}}}}}}resource "kubernetes_service" "test" {metadata {name = "nginx"namespace = kubernetes_namespace.test.metadata.0.name}spec {selector = {app = kubernetes_deployment.test.spec.0.template.0.metadata.0.labels.app}type = "NodePort"port {node_port = 30201port = 80target_port = 80}}}resource "kubernetes_ingress_v1" "test" {metadata {name = "test-ingress"namespace = "nginx"annotations = {"ingress.cloud.tencent.com/direct-access" = "false""kubernetes.io/ingress.class" = "qcloud""kubernetes.io/ingress.existLbId" = tencentcloud_clb_instance.ingress-lb.id"kubernetes.io/ingress.extensiveParameters" = "{\\"AddressIPVersion\\": \\"IPV4\\"}""kubernetes.io/ingress.http-rules" = "[{\\"path\\":\\"/\\",\\"backend\\":{\\"serviceName\\":\\"nginx\\",\\"servicePort\\":\\"80\\"}}]""kubernetes.io/ingress.https-rules" = "null""kubernetes.io/ingress.qcloud-loadbalance-id" = tencentcloud_clb_instance.ingress-lb.id"kubernetes.io/ingress.rule-mix" = "false"}# selfLink = "/apis/networking.k8s.io/v1/namespaces/nginx/ingresses/test-ingress"}spec {rule {http {path {backend {service {name = kubernetes_service.test.metadata.0.nameport {number = 80}}}path = "/"}}}}}
eks-app-singapore, and remove the original directory's reference to tfstate:$ mkdir ../eks-app-singapore$ cp *.tf ../eks-app-singapore$ cd ../eks-app-singapore
provider "tencentcloud" {# - replace# region = "ap-guangzhou"# + toregion = "ap-singapore"}
terraform init and terraform plan in the eks-app-singapore directory. As there is no tfstate file, terraform plan will prompt that resources will be created:Plan: 11 to add, 0 to change, 0 to destroy.───────────────────────────────────────Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
terraform apply to configure management of cloud resources in the new directory in the new region.datasource of each resource to query available instance specifications, instead of hard-coding the data in a file, for example:resource "tencentcloud_instance" "cvm" {name = "my-instance"availability_zone = "ap-shanghai-4"image_id = "local.cvm_img_id"instance_type = "S5.MEDIUM2"}
datasource as follows:provider "tencentcloud" {region = "ap-guangzhou"}# Query the AZs in Guangzhou region where CVM instances are availabledata "tencentcloud_availability_zones_by_product" "cvm" {product = "cvm"}# Query CVM images starting with `Tencent`data "tencentcloud_images" "img" {image_name_regex = "Tencent"}# Query the 2-core 2 GB MEM instance types in the specified AZdata "tencentcloud_instance_types" "types" {availability_zone = data.tencentcloud_availability_zones_by_product.cvm.zones.0.namecpu_core_count = 2memory_size = 2}locals {# Select the first result in the AZ listcvm_zone = data.tencentcloud_availability_zones_by_product.cvm.zones.0.name# Select the first result in the image listcvm_img_id = data.tencentcloud_images.img.images.0.image_id# Select the first result in the instance type listcvm_type = data.tencentcloud_instance_types.types.instance_types.0.instance_type}resource "tencentcloud_instance" "cvm" {name = "my-instance"availability_zone = local.cvm_zoneimage_id = local.cvm_img_idinstance_type = local.cvm_type}
Feedback