Cycles and unknown values
Recognize a cycle rather than trying to force an order
A cycle occurs when a resource depends on another that eventually depends back on it. In this intentionally invalid local example, each resource needs the output of the other.
resource "terraform_data" "left" { input = terraform_data.right.output}
resource "terraform_data" "right" { input = terraform_data.left.output}There is no first resource that satisfies both constraints. Terraform should reject this configuration rather than choose an arbitrary start. Adding depends_on or reducing parallelism cannot solve the circular relationship. Find the full chain, including locals, module inputs and outputs, before changing the design. The cycle fixture is saved as a separate invalid example; never apply it.
Break the cycle by changing the data flow
If the desired catalog name is actually known before creation, make it an independent input to the first resource. The following independent configuration keeps only the useful forward relationship.
resource "terraform_data" "left" { input = "books"}
resource "terraform_data" "right" { input = terraform_data.left.output}Left can now be created from books; right receives its output afterward. If both objects truly require each other’s generated identifiers, a structural redesign is needed: separate initial creation from a later association if the provider supports it, or use an independently known identifier. Do not delete state or remove a valid dependency simply to suppress a diagnostic. Check the provider’s lifecycle contract when redesigning real infrastructure.
Distinguish unknown values from errors
A plan can describe a resource even if a future result has not been assigned yet. Consider this separate fresh local configuration.
resource "terraform_data" "catalog" { input = "books"}
output "label" { value = "catalog-${terraform_data.catalog.id}"}The catalog ID is generated by Terraform during creation, so the complete label is expected to be unknown before the first apply. It is not the literal string null, not an empty string and not an error by itself. After creation the label should have the catalog- prefix followed by an ID; the exact ID is not deterministic. Later plans may already know it from state. A data source whose query needs an unavailable ID may have to wait until apply; its dependent values can also remain unknown. Unknown does not guarantee eventual success, and a failed lookup remains a possible error.
Keep instance identities known at plan time
A resource can accept an unknown input value, but Terraform needs to know which resource instances it is planning. This separate intentionally invalid example assumes a fresh state.
resource "terraform_data" "catalog" { input = "books"}
resource "terraform_data" "record" { for_each = toset([terraform_data.catalog.id]) input = each.key}The set member is also the instance key, so the address cannot be determined before creation. Use a known map key and put the generated ID in the value instead.
resource "terraform_data" "catalog" { input = "books"}
resource "terraform_data" "record" { for_each = { primary = terraform_data.catalog.id } input = each.value}Terraform can identify terraform_data.record["primary"] at plan time. Its input can remain unknown until the catalog exists. This change is about choosing a known identity, not making the ID itself available earlier. It is not a migration recipe for existing instances; review address changes separately.
Diagnose with a narrow, local workflow
Read the reported cycle chain and sketch each dependency. For an approved isolated fixture, the graph command supports an operation-specific cycle view.
terraform graph -type=plan -draw-cycles > cycle.dotThis is a diagnostic attempt, not a promise that an invalid configuration will produce a complete graph. Check the command status and error output. For unknown values, identify whether the value is merely a resource argument or part of count or for_each identity. The worksheet asks you to correct a cycle and distinguish unknown values from invalid keys. Continue to lifecycle and safe replacement after you can explain these two different planning problems.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cycles and unknown values — knowledge check
Check graph order, cycle diagnosis and plan-time identity.
What is wrong when left and right each require the other’s output?
What is a structural fix for the example cycle?
What does an unknown generated ID on the first plan mean?
Which for_each expression gives a predictable key in the fresh fixture?
Can a data query be deferred until apply?