Terrain import - import existing resources into terrain

summary

There is a prerequisite for terraform management infrastructure, that is, these infrastructures must be created by terraform, which has the status information of these resources.

However, in the real production environment, in most cases, we first have a lot of public cloud infrastructure, and then found that we can use terrain to manage public cloud infrastructure. In this case, since the previous public cloud infrastructure was not created by terraform, it cannot be managed without these resources in the state of terraform (these existing technical facilities cannot be added, deleted, modified or checked).

Fortunately, Terraform provides a way to import infrastructure resources not created by Terraform using the terraform import command.

Command format: terraform import [options] ADDRESS ID

#ADDRESS is the ADDRESS of the resource in the resource configuration file
#The ID is the ID of the real resource on the public cloud

Although terrain import can provide an import method to let us manage the resources that already exist before us. However, it is also inconvenient. When importing resources, you can't import them in batches. You can only import one resource at a time; And all resources must manually write resource configurations in the configuration file. These configurations cannot be generated automatically.

Import resource presentation

Pre preparation

1. First use the configuration file to create a VPC and a switch (subnet) in Alibaba cloud

Content of configuration file:

#Configure alicloud provider
provider "alicloud" {
    access_key = "your_ak_xxx"
    secret_key = "your_sk_xxx"
    region = "cn-shenzhen"
}

#Create alicloud vpc
resource "alicloud_vpc" "liqi-vpc-test" {
    vpc_name = "liqi-vpc-test"
    cidr_block = "10.100.0.0/16"
}

#Create Alibaba cloud vswitch with subnet of 10.100.1.0/24
resource "alicloud_vswitch" "liqi-vswitch-test" {
    vpc_id = "${alicloud_vpc.liqi-vpc-test.id}"
    cidr_block = "10.100.1.0/24"
    zone_id = "cn-shenzhen-b"
}

Console resources:

 

2. Manually create 2 switches (subnets) on the console

Two switches (subnets) manually created for my console in the red box

3. Compare terrain management resources

Terrain state list view resourcelist. You can see that there is only one vpc and switch we created through the configuration file

 

Add the resource configuration to import from the configuration file

Content of configuration file:

Add the resource configuration of the two switches (subnets) we want to import

#Configure alicloud provider
provider "alicloud" {
    access_key = "your_ak_xxx"
    secret_key = "your_sk_xxx"
    region = "cn-shenzhen"
}

#Create alicloud vpc
resource "alicloud_vpc" "liqi-vpc-test" {
    vpc_name = "liqi-vpc-test"
    cidr_block = "10.100.0.0/16"
}

#Create Alibaba cloud vswitch with subnet of 10.100.1.0/24
resource "alicloud_vswitch" "liqi-vswitch-test" {
    vpc_id = "${alicloud_vpc.liqi-vpc-test.id}"
    cidr_block = "10.100.1.0/24"
    zone_id = "cn-shenzhen-b"
}

#Import Alibaba cloud vswitch. The subnet is 10.100.2.0/24
resource "alicloud_vswitch" "liqi-vswitch-test2" {
    vpc_id = "${alicloud_vpc.liqi-vpc-test.id}"
    vswitch_name = "liqi-vswitch-test2"
    cidr_block = "10.100.2.0/24"
    zone_id = "cn-shenzhen-c"
}

#Import Alibaba cloud vswitch. The subnet is 10.100.3.0/24
resource "alicloud_vswitch" "liqi-vswitch-test3" {
    vpc_id = "${alicloud_vpc.liqi-vpc-test.id}"
    vswitch_name = "liqi-vswitch-test3"
    cidr_block = "10.100.3.0/24"
    zone_id = "cn-shenzhen-d"
}

 

Import resource

Although the resource of our configuration file directly writes the configuration of both switches (subnets). However, when we execute terrain import to import the resource state, we can only import one resource at a time.

terraform import alicloud_vswitch.liqi-vswitch-test2 vsw-wz9dun3xkcfxxxxxxxxxx

terraform import alicloud_vswitch.liqi-vswitch-test3 vsw-wz9xc10qio9xxxxxxxxxx

If the import is successful, a prompt will appear on the command line

 

View the imported resource state list

summary

Write the existing infrastructure as a resource configuration file and import it into Terraform. In this way, we can use Terraform to manage our cloud infrastructure.

Although the current import is not so intelligent and user-friendly (each resource configuration should be written by itself; only one resource can be imported at a time), Terraform is also constantly improving this part.

Tags: Cloud Native terraform

Posted by zigizal on Thu, 05 May 2022 13:59:27 +0300