如何将 terraform.tfstate 转换为配置文件?

Joh*_*hn 3 amazon-web-services terraform

我在 AWS 管理控制台中创建了一个 AWS 资源。然后我跑着terraform import将 AWS 资源导入 Terraform。现在我有了这个terraform.tfstate文件。但是如何将其转换回 Terraform 配置文件?

aja*_*xhe 5

可以用 terraform show 命令生成 tf 文件的原型吗?就这样?

# terraform show
Run Code Online (Sandbox Code Playgroud)

命令输出是?

# tencentcloud_instance.ajaxhe_ins:
resource "tencentcloud_instance" "ajaxhe_ins" {
    allocate_public_ip                      = true
    availability_zone                       = "ap-hongkong-2"
    create_time                             = "2020-01-23T11:09:28Z"
    expired_time                            = "2020-05-24T09:41:36Z"
    id                                      = "ins-59xsw9ji"
    image_id                                = "img-31tjrtph"
    instance_charge_type                    = "PREPAID"
    instance_charge_type_prepaid_renew_flag = "NOTIFY_AND_MANUAL_RENEW"
    instance_name                           = "centos-1GB--2170"
    instance_status                         = "RUNNING"
    instance_type                           = "S2.SMALL1"
    internet_charge_type                    = "BANDWIDTH_PREPAID"
    internet_max_bandwidth_out              = 1
    private_ip                              = "172.18.1.0"
    project_id                              = 0
    public_ip                               = "129.173.115.221"
    running_flag                            = true
    security_groups                         = [
        "sg-lxlzf8fn",
    ]
    subnet_id                               = "subnet-3a05z4r3"
    system_disk_id                          = "disk-b0p7allu"
    system_disk_size                        = 50
    system_disk_type                        = "CLOUD_PREMIUM"
    tags                                    = {}
    vpc_id                                  = "vpc-g3q13u9g"
}
Run Code Online (Sandbox Code Playgroud)

然后?删除不可更改的配置,例如:id、public_ip、instance_status等。

最终的 main.tf 文件可能是这样的:

# tencentcloud_instance.ajaxhe_ins:
resource "tencentcloud_instance" "ajaxhe_ins" {
    allocate_public_ip                      = true
    availability_zone                       = "ap-hongkong-2"
    create_time                             = "2020-01-23T11:09:28Z"
    image_id                                = "img-31tjrtph"
    instance_charge_type                    = "PREPAID"
    instance_charge_type_prepaid_renew_flag = "NOTIFY_AND_MANUAL_RENEW"
    instance_name                           = "centos-1GB--2170"
    instance_status                         = "RUNNING"
    instance_type                           = "S2.SMALL1"
    internet_charge_type                    = "BANDWIDTH_PREPAID"
    internet_max_bandwidth_out              = 1
    private_ip                              = "172.18.1.0"
    project_id                              = 0
    running_flag                            = true
    security_groups                         = [
        "sg-lxlzf8fn",
    ]
    subnet_id                               = "subnet-3a05z4r3"
    system_disk_id                          = "disk-b0p7allu"
    system_disk_size                        = 50
    system_disk_type                        = "CLOUD_PREMIUM"
    tags                                    = {}
    vpc_id                                  = "vpc-g3q13u9g"
}
Run Code Online (Sandbox Code Playgroud)