hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
42.78k stars 9.56k forks source link

Config-driven import | generate code for modules, too #33376

Open black-snow opened 1 year ago

black-snow commented 1 year ago

Terraform Version

1.5.0

Use Cases

I love the new import block. Sadly, it seems to be less useful for modules as:

  1. import has to be at root level
  2. cannot import into (generate code for) stuff within modules

My use case is not only to import but also have the corresponding code generated (which works fine for top-level resources).
My current workaround is

This usually works but involves a lot of manual steps, and I think the block was intended to remove that manual burden. Maybe I'm missing something here.

Attempted Solutions

current workaround s. a.

Proposal

I'd like tf to generate code for modules as well. Ideally, I'd just point the import to a non-existing resource within a module (local one) and the existing file or folder and tf would generate the code and append it to the file or create a file within the given folder.

I may fail for non-local modules, of course.

References

No response

omarismail commented 1 year ago

Hey @black-snow , thank you for this request!

We do intend on supporting import{} natively within modules, but are still working through the design. I'd be happy to chat more about this in detail! Please email me at oismail@hashicorp.com and would love to setup a time to talk :).

Vermyndax commented 9 months ago

@black-snow could you give a little more data on your workaround? We usually create our Terraform based on community modules only, and have to have a pretty big justification for writing and maintaining our own modules. We'd like to use the new import functionality to bring existing resources into community-based modules, but we don't see an easy way to do this right now.

black-snow commented 9 months ago

@Vermyndax I don't see how this could currently work. I'd do it manually or write a short pre-processor if it's too much to handle manually.

TrueCyberAxe commented 8 months ago

Is there still no plans to support it within child modules?

https://github.com/hashicorp/terraform/issues/33474 <--- i have posted a number of scenarios where it would be helpful for us

It's pretty useless as is being limited to the root module, solves a lot of issues with the ability to use it in child modules.

If the issue is "safety", that's why atlas has a built in plan step so you can review the changes before they are applied.

omarismail commented 8 months ago

@TrueCyberAxe @black-snow @Vermyndax

I'd love to chat through this use case more. Could you email me and we can find time to chat? oismail@hashicorp.com