Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert terraform.tfstate to config file?

I created an AWS resource in the AWS management console. I then ran terraform import to import the AWS resource into Terraform. Now I have this terraform.tfstate file. But how can I convert this back to a Terraform configuration file?

like image 459
John Avatar asked Dec 13 '22 17:12

John


1 Answers

You can use terraform show command to generate the prototype of tf file,just like this:

# terraform show

The command output is:

# 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"
}

then,remove the unchangeble configure, such as: id, public_ip, instance_status etc.

the final main.tf file may like this:

# 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"
}
like image 102
ajaxhe Avatar answered Jan 14 '23 12:01

ajaxhe