Do we have any tool to generate the resource block for the resource that is imported into the terraform state?

Do we have any tool to generate the resource block for the resource that is imported into the terraform using terraform import command? If not, is HashiCorp is planning to improve this feature for the users?

There are some third party tools such as terraforming which attempt to do this for some types of resource, but it isn’t something that the core Terraform does. While it can sometimes be useful for a few situations I actually find that in general auto-generated code doesn’t save any time. There are often different ways of representing the same data (for example security group rules as separate resources compared with built into the main security group resource) which any automated tool wouldn’t be able to choose the most appropriate option. Also you often find that count or for_each would be the best option, which tools wouldn’t use themselves. Finally resources contain lots of linking IDs (e.g. a VPC linked with a subnet) which just end up being created as fixed strings. With Terraform it is vital to have things properly liked together using resource references, so that the dependency graph is created correctly. Overall ant code automatically produced ends up requiring substantial work, and sometimes it would have been quicker to have started from scratch.

Instead I generally find that a good method is to create bare bones resources that you need (in the style that works for your use case) and then use terraform import to add the existing resources to the state file. Running terraform plan will then show differences between your code & reality, which you can represent through adjusting your code (or often you find that reality actually needs changing because it doesn’t fit with what you actually want to happen)

1 Like

Hey @rohit-myali and @stuart-c

I appreciate both of your thoughts! My name is Omar and I’m the PM for Terraform.

Yes HashiCorp is planning on improving the import workflow, and also stuart is making an important point that its very hard to build a perfect solution. A developer should always be reviewing anything generated to make sure that the configuration represents their intentions before applying.

I’d love to spend some time chatting with you both to discuss your workflows and share some things we are working. If you are up for it, we can chat via email or have a live chat. My email is Talk soon!