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.36k stars 9.5k forks source link

import block: generate config within local modules #35596

Open vazome opened 1 month ago

vazome commented 1 month ago

Terraform Version

Terraform v1.9.4
on darwin_arm64
+ provider registry.terraform.io/hashicorp/azurerm v3.116.0

Terraform Configuration Files

module "azure-rg" {
  source = "./modules/azure-rg"
}

provider "azurerm" {
  features {}
  tenant_id = var.tenant_id
  subscription_id = var.subscription_id
}

import {
  to = module.azure-rg.azurerm_resource_group.rg-databricks
  id = "/../resourcegroup../"
}

Debug Output

https://gist.github.com/vazome/d639adc98c0999d0edae4abf80f4a816

Expected Behavior

Terraform creates resource configuration file for the given import

Actual Behavior

│ Error: Configuration for import target does not exist │ │ on main.tf line 15, in import: │ 15: to = module.azure-rg.azurerm_resource_group.rg-databricks │ │ The configuration for the given import target module.azure-rg.azurerm_resource_group.rg-databricks does not exist. All target │ instances must have an associated configuration to be imported.

Steps to Reproduce

terraform plan -generate-config-out=generated_resources.tf

Additional Context

This issue seems to be covered in this Japanese blog

References

No response

crw commented 1 month ago

Thanks for this report! 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!

liamcervante commented 1 month ago

Related to https://github.com/hashicorp/terraform/issues/33376

liamcervante commented 1 month ago

I've relabelled this as an enhancement instead of a bug.

There are three kinds of modules: local, registry and remote. Generating configuration for registry and remote based modules from the root module is not something that will ever be possible. It does seem possible that we could add support for generating configuration within local modules.

It's worth noting there are considerable complications with this given the -generate-config-out command points to a single file location. We'd need to update that to allow specifying the file within the local module, and then also make sure there aren't import blocks requesting configuration generation across multiple modules as the file will only be written to a single module. Potentially, the flag could support multiple file locations with a requirement that every module that wants to generate config needs to have a file included.

A workaround here would be to generate the configuration at the root level, and then move the generated configuration into the local module, before rerunning the plan command to connect the import block up to the correct module.

If you're interested in this as a feature, please give a 👍 reaction to the original comment!