Search code examples
azureterraformterraform-provider-azure

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


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?


Solution

  • If you are looking to import Azure resources then aztfy is the recommended tool as it is natively from Azure.

    It does generate the terraform code, additionally, it has a feature where you can import the azure resource group, it automatically imports and generates config for the resources that the resource group is holding. Not to mention but the tool gives you a nice terminal-based-UI experience.

    For other hyperscalers, there are two choices.

    • terracognita : can generate modules too as per their docs.
    • terraformer : Developed by Google people but not official product.