John
John

Reputation: 169

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?

Upvotes: 4

Views: 10583

Answers (2)

ajaxhe
ajaxhe

Reputation: 609

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

Upvotes: 18

ydaetskcoR
ydaetskcoR

Reputation: 56877

As the terraform import docs explain, currently Terraform will only import the resource into your state file and won't generate the config for you.

If you try this without even defining the resource Terraform will throw an error telling you to define the resource and even providing a bare bones example:

Error: resource address "aws_instance.foo" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "aws_instance" "foo" {
  # (resource arguments)
}

If you were to then take that config and put that into a .tf file for Terraform to use it should import fine but a plan will tell you that it's missing required fields:

Error: aws_instance.foo: "ami": required field is not set

Error: aws_instance.foo: "instance_type": required field is not set

If you then add those required fields and run a plan again Terraform will then show you the diff you have between the resource you imported and the changes the config wants to apply. You probably then want to go back to your config and align these so your plan is empty and then apply it. At that point it's as if Terraform originally created the resource and will then proceed to manage it as part of the resource lifecycle.

Upvotes: 5

Related Questions