terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Config-driven import | generate code for modules, too

Open black-snow opened this issue 1 year ago • 3 comments

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

  • to point the import at top level
  • run plan with code generation
  • move code into module
  • point import to the resource within the module
  • apply

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

black-snow avatar Jun 15 '23 13:06 black-snow

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 [email protected] and would love to setup a time to talk :).

omarismail avatar Jun 15 '23 17:06 omarismail

@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.

Vermyndax avatar Feb 15 '24 19:02 Vermyndax

@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.

black-snow avatar Feb 18 '24 12:02 black-snow

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.

TrueCyberAxe avatar Mar 07 '24 15:03 TrueCyberAxe

@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? [email protected]

omarismail avatar Mar 07 '24 22:03 omarismail