The Terraform docs for some weird reason do not explain what "Error: Cycle" means. I've looked everywhere but there is no mention of it on the official docs. (Turns out it is well-known term, a circular dependency, that someone apparently renamed thinking it would make them sound cool...)
As part of Terraform's work, it analyzes the dependencies between your
resource
blocks,data
blocks, and other configuration constructs in order to determine a suitable order to process them so that the necessary input data will be available.For example, consider the following contrived simple configuration:
Terraform will analyze the above and notice that the configuration of
null_resource.bar
contains a reference tonull_resource.foo
, and therefore the operations related tonull_resource.foo
must happen beforenull_resource.bar
. We can visualize that as a graph where an arrow represents "must happen after", or "depends on":null_resource.bar
must happen after operations fornull_resource.foo
.Consider now what happens if we modify that configuration like this:
Now
null_resource.foo
also refers tonull_resource.bar
. There are now two "must happen after" relationships implied by this configuration:null_resource.bar
must happen after operations fornull_resource.foo
.null_resource.foo
must happen after operations fornull_resource.bar
.The two statements above contradict one another:
null_resource.bar
cannot be processed both before and afternull_resource.foo
. Terraform will respond to this situation by reporting a dependency cycle, using the error message you've seen:When Terraform returns this error, the solution is to remove at least one of the "must happen after" arrows (dependencies) from the configuration so that it's no longer contradictory. Without seeing your configuration I can't suggest what specific change might achieve that in your case, but it's likely that somewhere in your configuration you have two mutually-dependent resources like this, or perhaps a resource referring to itself.
If you are sighted, depending on how complicated your configuration is, it might help to ask Terraform to produce a graph similar to the ones I've included above in this answer, but highlighting the cycles. To do this, you can use the
terraform graph
command, like this:The output of this command is a description of the graph in the format accepted by Graphviz. If you don't have Graphviz installed on your local computer, you could instead copy-paste the output into Graphviz Online to produce a graph image. The
-draw-cycles
command causes Terraform to mark the arrows that are related to the cycle being reported using the color red. If you cannot visually distinguish red from black, you may wish to first edit the generated Graphviz code to replacered
with some other color you can distinguish.The graph visualization of the configuration tends to become unusable for non-trivial configurations because there are so many graphs and edges, so if your configuration has many objects it may be better to follow the dependencies through the configuration itself.