Shan
Shan

Reputation: 2221

Terraform initial state file creation

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,

  1. What is the best way to migrate an existing AWS Infrastructure to Terraform code
  2. Is it possible to add a resource manually and modify the state file manually (bad effects ?)

Update

Terraform 0.7.0 supports importing single resource.

Upvotes: 9

Views: 2218

Answers (2)

ydaetskcoR
ydaetskcoR

Reputation: 56839

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.

Update

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 plans (and applys) should show no changes to the resource and happily keep the imported resource.

Upvotes: 5

padmakarojha
padmakarojha

Reputation: 335

Use Terraforming https://github.com/dtan4/terraforming , To date it can generate most of the *.tfstate and *.tf file except for vpc peering.

Upvotes: 1

Related Questions