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.76k stars 9.56k forks source link

Evaluation of module not needed when module is not deployed #32244

Closed steini44as closed 1 year ago

steini44as commented 1 year ago

Terraform Version

Terraform v1.2.3
on windows_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.14.0

Terraform Configuration Files

resource "azurerm_virtual_network" "vnet" {
  name                = var.ResourceName
  resource_group_name = var.ResourceGroupName
  location            = var.Location
  address_space       = var.VNetAddressList
  dns_servers         = var.DnsServerIPAddressList == [""] ? null : var.DnsServerIPAddressList
}
module "virtual_network_hub"{
  count = local.var_config.VirtualNetworkHubObject == {} ? 0 : 1
  source = "./modules/virtual_network"
  ResourceName = local.var_config.VirtualNetworkHubObject.VirtualNetworkName
  ResourceGroupName = azurerm_resource_group.rg[local.var_config.ResourceGroupName].name
  Location = local.var_config.Location
  VirtualNetworkAddressList = local.var_config.VirtualNetworkHubObject.VirtualNetworkAddressList
  DnsServerIPAddressList = lookup(local.var_config.VirtualNetworkHubObject,"DnsServerIPAddressList", null) != null ? local.var_config.VirtualNetworkHubObject.DnsServerIPAddressList : null
}

Debug Output

none

Expected Behavior

If VirtualNetworkHubObject is empty, it shouldn't error/evaluate the ResourceName, ResourceGroupName etc).

Actual Behavior

Error: Unsupported attribute │ │ on main.tf line 54, in module "virtual_network_hub": │ 54: ResourceName = local.var_config.VirtualNetworkHubObject.VirtualNetworkName │ ├──────────────── │ │ local.var_config.VirtualNetworkHubObject is object with no attributes │ │ This object does not have an attribute named "VirtualNetworkName".

Now it tries to look for VirtualNetworkName inside the VirtualNetworkHubObject object.

Steps to Reproduce

  1. 'terraform init'
  2. 'terraform appy'

Additional Context

No response

References

No response

jbardin commented 1 year ago

Hi @steini44as,

Terraform decodes and validates the configuration as a whole before applying any of the specific configuration values. Since this is a known type violation, the validation process catches the error and reports it correctly. Because not all values can be known during validation, Terraform does not make any assumptions about how modules will be expanded, and all configuration is processed. The only way to fully remove a module from being evaluated is to remove it from the configuration.

Since Terraform is working as designed here, and this does not represent a bug in the CLI, I'm going to close this out. We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help.

Thanks!

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.