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

Allow referencing one instance of a resource from another instance of that same resource #22517

Open
cezarsa opened this issue Aug 19, 2019 · 2 comments

Comments

@cezarsa
Copy link

cezarsa commented Aug 19, 2019

Terraform Version

$ terraform -v
Terraform v0.12.6
+ provider.null v2.1.2

Terraform Configuration Files

resource "null_resource" "vm" {
  count = 2

  triggers = {
    order = count.index > 0 ? null_resource.vm[count.index - 1].id : ""
  }
}

Debug Output

https://gist.github.com/cezarsa/a309d2a0a7ff7b00be749598a1af4707

Expected Behavior

In the config file shown above I expected no cycle to exist. It should simply be null_resource.vm[1] depending on null_resource.vm[0] and no dependencies the other way around.

Actual Behavior

Running terraform plan causes the error:

Error: Cycle: null_resource.vm[1], null_resource.vm[0]

Steps to Reproduce

  1. Run terraform plan
@teamterraform teamterraform changed the title Self referencing resource on another count index causes incorrect cycle Allow referencing one instance of a resource from another instance of that same resource Aug 19, 2019
@teamterraform
Copy link
Contributor

Hi @cezarsa!

Terraform is working as intended here because the references are resolved between the resource blocks as a whole rather than between the individual instances. There is currently no way to reference between instances of the same resource, and so this would instead need to be written as multiple separate resources without count set, or some other design like this where the provisioner is attached to a separate resource that depends on the one that has count set:

resource "null_resource" "a" {
  count = 2
}

resource "null_resource" "b" {
  # Access null_resource.a in here to get the list of all instances, which you
  # can then combine in whatever way makes sense.
}

We asked the bot to mark this as an enhancement to represent the use-case of referencing one instance of a resource from another instance of the same resource. That is architecturally impossible right now, but having this issue representing the idea will allow others to potentially share use-cases.

If you are able to share more, it would be helpful to know what real problem you were trying to solve with this, since having real examples helps us to evaluate many potential solutions to a problem.

@salexpdx
Copy link

I came across this issue when I was searching due to a cycle error that i have. I was looking for a way to combine multiple JSON IAM documents into a single document that was the merge of all of them. The goal was to make it so that I didn't have a fixed number of policies that were hardcoded and just repeating the same thing over and over again to step through the list. My code looked something like this:

data "aws_iam_policy_document" "policies" {
  # Need to create one copy for each item in the var.documents list
  count = length(var.documents)
  # If this is the the first item in the list, then just use an empty document as the source json, 
  # otherwise, use our previous item in the list.  
  source_json = count.index == 0 ? "{}" : data.aws_iam_policy_document.policies[count.index - 1]
  # If the index count is less than the number of items in var.documents, then use the entry in 
  # var.documents for this index, otherwise just use an empty json doc.
  override_json = count.index < length(var.documents) ? var.documents[count.index] : "{}"
}

When I tried to run the above, it gives the cycle error like above and stops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants