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.6k stars 9.54k forks source link

auto generate optional import blocks for a module #33427

Open bt-macole opened 1 year ago

bt-macole commented 1 year ago

Terraform Version

Terraform 1.5.1

Use Cases

  1. Support optional/dynamic import blocks that can use variables.
  2. Auto generate import blocks for all resources in a terraform module

Attempted Solutions

Writing import blocks for a module on the consumer end and manually tracking down the correct resource names and import values.

Proposal

config-driven imports is great, the native use with traditional cicd via plan and apply is super powerful!

To make it more robust and support module development that takes full advantage of this feature import blocks should be dynamic and ideally auto generated.

It would be awesome if imports could be dynamic like other terraform blocks:

dynamic “import” {
  for_each = local.imports
  contents {
    id    = import.value.id
    key = import.value.key
  }
}

it would also be great if the import block(s) could be auto generated for resources in a module via cli, something like: terraform generate imports

could generate import blocks for resources in a module with all the required import values using the correct resource names/paths.

References

No response

crw commented 1 year ago

Thanks for this request!

ronaldmiranda commented 1 year ago

That feature would be super interesting

oded-e commented 8 months ago

Thanks! I assume this request is only for existing infra. Isn't it? I hope we'll see that in the next few versions

bt-macole commented 8 months ago

This is actually for static use in modules. The two use cases happen at different levels of abstraction, but are complementary, and together, provide auto generated optional import statements in terraform modules.

For example:

  1. As a terraform module developer, I want the ability to include optional import statements in my module so that users can provide variables to trigger imports with my module.

This would mean a module developer could include the import blocks like dynamic blocks providing optional imports that abstract resource names (id) and import syntax, enabling users to easily import with whatever key is needed. For resources that have a calculable key before apply, this means the import could be flagged on with a bool and the module developer could use the already provided inputs to derive the key without a user needing to understand any extra implementation details, WYSIWYG.

  1. As a terraform module developer, I would like to auto generate scaffolding for dynamic import blocks, so that I can easily provide this to end users without needing to go look at what the specific resource's key is.

The attributes required to achieve an import are defined at the provider level. Import blocks have a very common shape to them.

To accelerate development of modules and provide a further enhanced developer experience, developers should be able to run a scaffolding command that generates the import statements with the known id (since resource names are statically typed + optional key indices) along with placeholders and/or comments that guide developers on what the key is--or derive keys from the module automatically if known before apply.


This provides a more robust terraform module that can leverage the newest features of terraform to the fullest. And provides an even better user experience to module developers and their users.