Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute-level circular dependency detection #29464

Closed
mikeclayton opened this issue Aug 27, 2021 · 2 comments
Closed

Attribute-level circular dependency detection #29464

mikeclayton opened this issue Aug 27, 2021 · 2 comments
Labels
enhancement new new issue not yet triaged

Comments

@mikeclayton
Copy link

mikeclayton commented Aug 27, 2021

Current Terraform Version

1.0.5

Proposal

Enhance circular dependency detection to allow some scenarios where terraform plan currently throws an error, but could actually resolve values.

For example:

resource "azurerm_virtual_machine" "my_az_vm" {
  name                  = "my-az-vm"
  network_interface_ids = [
    # creates a dependency on my_az_vm_nic
    azurerm_network_interface.my_az_vm_nic.id
  ]
  ... etc ...
}

resource "azurerm_network_interface" "my_az_vm_nic" {
  # creates a dependency on my_az_vm
  name = "${azurerm_virtual_machine.my_az_vm.name}-nic"
  ... etc ...
}

fails with the following error:

Error: Cycle: azurerm_network_interface.my_az_vm_nic, azurerm_virtual_machine.my_az_vm

In this case, some attributes in my_az_vm refer to attributes in my_az_vm_nic and vice versa, so there's a circular dependency detected at the resource level, but strictly speaking there's no circular reference at the attribute level because the value of azurerm_virtual_machine.my_az_vm.name could potentially be determined during the plan phase (here it's a literal string, but I could imagine it being an arbitrary expression which references other attributes and invokes arbitrary Configuration Language functions as long as it doesn't create an attribute-level cycle).

To work around this at present I've created a local value and referenced that in both resources to break the cycle:

locals {
  virtual_machine_name = "my-az-vm"
}

resource "azurerm_virtual_machine" "my_az_vm" {
  name                  = local.virtual_machine_name
  network_interface_ids = [
    # creates a dependency on my_az_vm_nic
    azurerm_network_interface.my_az_vm_nic.id
  ]
  ... etc ...
}

resource "azurerm_network_interface" "my_az_vm_nic" {
  name = "${local.virtual_machine_name}-nic"
  ... etc ...
}

but it would nice to be able to use the original version above ^^^.

This might all have implications on other operations - e.g. terraform destroy would need to be able to decide which resource to destroy first, so I realise this isn't just a simple request, but your thoughts welcome...

References

A search in this repo for "cycle issues" (https://github.com/hashicorp/terraform/search?q=cycle&type=issues) finds a couple of related (but not identical) issues:

resource "azurerm_virtual_machine" "my_az_vm" {
  # creates a dependency on my_az_vm_nic
  name = azurerm_network_interface.my_az_vm_nic.name
  ... etc ...
}

resource "azurerm_network_interface" "my_az_vm_nic" {
  # creates a dependency on my_az_vm
  name = azurerm_virtual_machine.my_az_vm.name
  ... etc ...
}

I'm not really asking for this scenario to be allowed, just attribute-level "cycles" that can be actually resolved like my first example ^^^.

Terraform is working as intended here because the references are resolved between the resource blocks as a whole rather than between the individual instances.

Tracking attribute-level cycles rather than resource-level cycles could potentially resolve this issue as a side-effect, which would be a happy accident, but I'm not specifically asking for that to be included in this feature request:-).

arguments inside a block can be evaluated in any order while implementing this feature would effectively make the arguments themselves a dependency graph, which is a considerably more complex design

@mikeclayton mikeclayton added enhancement new new issue not yet triaged labels Aug 27, 2021
@mikeclayton mikeclayton changed the title Attribute-level circular dependency resolution Attribute-level circular dependency detection Aug 27, 2021
@jbardin
Copy link
Member

jbardin commented Aug 30, 2021

Hi @mikeclayton,

Thanks for filing the issue. While in the case presented above, it would be theoretically possible for terraform to break the cycle and evaluate that specific value, there would still be no way to plan or apply the resources because a suitable order cannot be determined.

References between resources not only allow one to share information between resources, but they also declare the dependencies required for planning and applying those resources. The references you are using here would be roughly equivalent to adding each resource to a depends_on list in the other, which should make it more clear why that cannot work. When multiple resources both depend on the same value, the correct solution is to break that value out into an independent location to prevent a cycle, which means what you have presented as the workaround is the expected structure of the configuration in this case.

Since this is working as intended, and there really isn't any way to infer references without creating dependencies within the design of Terraform, I'm going to close this out. If you have more questions, it's better to use the community forum where there are more people ready to help.

Thanks!

@jbardin jbardin closed this as completed Aug 30, 2021
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants