How to import all resources into state and generate terraform config file into JSON format

Hi folks,
I have a use case where I have to import all resources into state at once and generate a JSON format configuration file that I can use later.
Terraform latest release 1.5.1 has config driven import functionality which is generating config for defined resource (Not in JSON format) but still I can work around with that. Is there a way that I can use this feature to fulfill my requirement ?

Hi @deeppatel318,

Config-driven import, at least for this first release, is for importing individual objects whose IDs you can name in the configuration directly.

You will need to use a third-party tool if your goal is to automatically discover existing infrastructure objects within a particular scope and import them. Some third-party tools are working to integrate with the new config-driven import by generating import blocks at the same time as they generate resource blocks, so that you can then just apply the change to get all of the new objects imported.

This problem of discovering existing objects that Terraform doesn’t already know about is necessarily platform-specific, since each cloud platform has its own special API for finding all objects matching a particular scope. Therefore these third-party import tools tend to be platform-specific too.

For example, Azure maintains aztfexport which can discover objects in Azure and recently added support for generating import blocks automatically as part of that process.

These tools do typically generate the main Terraform language syntax instead of the JSON variant, because they are assuming that the goal of importing into Terraform is to allow humans to directly maintain the code once imported. I don’t know if a tool that directly generates the JSON syntax.

@apparentlymart Thank you, Let me try to use this third party tool GitHub - Azure/aztfexport: A tool to bring existing Azure resources under Terraform's management.