New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new terraform regenerate
command
#34890
Comments
Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again! |
The primary application is for refactoring; thus, the command might actually be named The idea is that you don't want to create nor destroy anything, just take all existing resources deployed by your workspace and put them under a single |
Thanks for sharing this proposal, @garysassano. You've presented it as a new command, which is a potentially-viable way to expose it, but I think for initial discussion I'd rather focus on the details of what this operation includes rather than how it is called or what it is named, since I hope that pinning down exactly what this operation would be doing will then lead to clearer options about how to offer it, whether that be as a new command, a new planning option, or something else entirely. I'd like to start by restating what I understood from your proposal in terms of the concepts in the resource instance change lifecycle. The idea as I understood it is to visit each resource instance that is either declared in the configuration or already tracked in the state, and for each one:
The most significant challenge in the above is in step 1, because Terraform currently has no way to transform a prior state into an import ID. Providers are in full control of both their import IDs and their state data formats, and so Terraform currently relies on the If that problem were solved then I think the rest of it is relatively straightforward, although there are still some minor edge-cases to consider:
Having run that thought experiment in my head, I'm left with a question: why is this extra step necessary, vs. just running The only extra thing this new command would do is losing any information that the provider can't reconstruct based on the API response. Re-importing the existing object is at best a no-op (the new state is identical to the old), but is often messier and lossier than that, causing the provider to lose track of anything the remote API can't track. Therefore if there's a situation that |
Use Cases
I'm interested in a command that combines
removed
(v1.7) andimport
(v1.5) blocks functionalities into a single operation. This command, potentially namedterraform regenerate
, would initiate a Terraform destroy plan for the entire workspace as if each resource was placed inside aremoved
block. It would then automatically extract the Import ID for each resource, take those IDs and feed them intoimport
blocks, thereby generating a new Terraform state file from the existing one.The primary benefit of this approach is that it gets rid of all existing
moved
blocks, while also dissolving all previously established modules. Essentially, it allows for an easy refactoring of your Terraform repository, moving from a complex and cluttered state into a streamlined structure. By the end of this process, you'd consolidate all resources from the previous configuration into a single.hcl
file. For instance, if your workspace initially consisted of 10 different modules and 20 directories, generating a total of 100 resources, you would now manage the same 100 resources within a singular.hcl
file, which you can then start to split at occurence.Attempted Solutions
see above
Proposal
No response
References
No response
The text was updated successfully, but these errors were encountered: