How to have terraform import all of the "already exists" resources automatically?

Viewed 4661

When I run terraform apply -auto-approve I get the following error:

Error: A resource with the ID "/subscriptions/.../resourceGroups/RG-SCUSTFStorage" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information.

I underestand that I need to run terraform import to import the resource to my worksapce. The problem is that I need to specify the resource id for all of the missing resources one at a time.

Is any way to have terraform import import all of the "already exists " resources automatically witout entering the resource IDs one at a time?

1 Answers

Unfortunately, you only can import the existing resources one by one with the resource IDs manually:

The import command doesn’t automatically generate the configuration to manage the infrastructure, though. Because of this, importing existing infrastructure into Terraform is a multi-step process.

More details here. I will suggest you use remote state storage for all the Terraform scripts before deployment. If you do not have the state file that contains all the deployed resources, then you only can import them one by one.

Related