Need to run multiple "terraform imports" with same tfstatefile

We want to improve the performance of importing multiple resources.

For the same we tried invoking multiple terraform imports from golang routines. Sometimes it imports only 1 resource and sometimes it doesnt import anything. The probable reason for the same is the race condition for writing the import results to “terraform.tfstate” file.

We want to know if terraform can run multiple terraform imports to different .tfstate files and merge it to single file as final output.

The state file is a relatively straightforward structure, but isn’t something that is recommended to be directly manipulated. So you could probably make something work, but you wouldn’t get any support and might find things change as providers and the core updates.

What is your use case for needing much faster import performance? Generally import is infrequently used, as you’d normally be creating infrastructure via Terraform directly. Import of existing resources would therefore be a one-off initial process.

2 Likes

Hi , Sorry for the late reply.
Our infrastructure isn’t deployed via Terraform. Hence in order to detect configuration drift, we are doing TF import of all the resources (each time as the resources keeps changing).
In order to minimize the TF import execution time, we were thinking of alternative methods.

So you are throwing away the state file each time?

That sounds very different to the use case Terraform is generally designed for. The idea is that the state file is persistent (often stored in one of the remote state methods). Equally if you are writing Terraform code it would be normal to then deploy via Terraform. Resources not managed using Terraform are then referenced using data resources.

If you aren’t using Terraform to handle deployments what are you using? Why are you wanting to use Terraform as well?