Is there a way to create the terraform state file, from the existing infrastructure. For example, an AWS account comes with a some services already in place ( for ex: default VPC).
But terraform, seems to know only the resources, it creates. So,
Terraform 0.7.0
supports importing single resource.
Terraform is a coding tool aligned with this practice which allows us to define cloud and local resources in formatted configuration files that we can reuse and share. When we build infrastructure in Terraform configuration, a state file gets created in the local workspace directory named terraform. tfstate .
This state is stored by default in a local file named "terraform. tfstate", but it can also be stored remotely, which works better in a team environment. Terraform uses this local state to create plans and make changes to your infrastructure.
Terraform stores information about your infrastructure in a state file. This state file keeps track of resources created by your configuration and maps them to real-world resources.
For relatively small things I've had some success in manually mangling a state file to add stubbed resources that I then proceeded to Terraform over the top (particularly with pre-existing VPCs and subnets and then using Terraform to apply security groups etc).
For anything more complicated there is an unofficial tool called terraforming
which I've heard is pretty good at generating Terraform state files but also merging with pre-existing state files. I've not given it a go but it might be worth looking into.
Since Terraform 0.7, Terraform now has first class support for importing pre-existing resources using the import
command line tool.
As of 0.7.4 this will import the pre-existing resource into the state file but not generate any configuration for the resource. Of course if then attempt a plan
(or an apply
) Terraform will show that it wants to destroy this orphaned resource. Before running the apply
you would then need to create the configuration to match the resource and then any future plan
s (and apply
s) should show no changes to the resource and happily keep the imported resource.
Use Terraforming https://github.com/dtan4/terraforming , To date it can generate most of the *.tfstate and *.tf file except for vpc peering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With