对于老用户,常常有把控制台上已有的资源导入terraform的需求。本篇以把控制台上已有的eip资源为例,介绍如何导入已有资源到terraform编排中。同时将该资源与tf中创建的NAT网关相绑定。
首先看一个terraform中的创建eip和nat 并绑定的tf文件示例。
代码语言:javascript复制data "tencentcloud_vpc_instances" "foo" {
name = "Default-VPC"
}
# Create EIP
resource "tencentcloud_eip" "eip_dev_dnat" {
name = "terraform_test"
}
resource "tencentcloud_eip" "eip_test_dnat" {
name = "terraform_test"
}
resource "tencentcloud_nat_gateway" "my_nat" {
vpc_id = "${data.tencentcloud_vpc_instances.foo.instance_list.0.vpc_id}"
name = "terraform_test"
max_concurrent = 3000000
bandwidth = 500
assigned_eip_set = [
"${tencentcloud_eip.eip_dev_dnat.public_ip}",
"${tencentcloud_eip.eip_test_dnat.public_ip}",
]
}
这个例子里,如何把控制台上的已存在的eip import到这个文件里, 并绑定的my_nat中?
步骤1:控制台新建或者找到需要import到terraform的eip实例(注意名称不可以为空)
步骤2:在上面的tf文件中,新建一个空的resource用来放置这个需要导入的eip
步骤3:打开命令行,执行import命令: terraform import tencentcloud_eip.eip_test_import(上一步在tf中新建的空resource名称) eip0-xxxx(import的eipID)
代码语言:javascript复制terraform import tencentcloud_eip.eip_test_import eip-xxxxxx
步骤4:import成功后,相关状态已记载到tfstate文件中,打开tfstate文件,找到tfstate中引入的resource的状态
步骤5:把eip资源的唯一属性name的值编辑到tf文件中,并且在my_nat资源内加入绑定ip
步骤6:执行terraform apply。输入执行yes。等待绑定成功。
最终的tf:
代码语言:javascript复制data "tencentcloud_vpc_instances" "foo" {
name = "Default-VPC"
}
# Create EIP
resource "tencentcloud_eip" "eip_dev_dnat" {
name = "terraform_test"
}
resource "tencentcloud_eip" "eip_test_dnat" {
name = "terraform_test"
}
#import EIP
resource "tencentcloud_eip" "eip_test_import" {
name = "test-import"
}
resource "tencentcloud_nat_gateway" "my_nat" {
vpc_id = "${data.tencentcloud_vpc_instances.foo.instance_list.0.vpc_id}"
name = "terraform_test"
max_concurrent = 3000000
bandwidth = 500
assigned_eip_set = [
"${tencentcloud_eip.eip_dev_dnat.public_ip}",
"${tencentcloud_eip.eip_test_dnat.public_ip}",
"${tencentcloud_eip.eip_test_import.public_ip}",
]
}