Definition
Terraform
/OpenTofu
is a declarative tool, and therefore when describing resources it is very important to understand the sequence of their creation, for this purpose hidden dependencies are automatically built between resources, by the way, performing a traversal of the dependency tree, Terraform/Tofu tries to parallelize the creation of resources whenever possible, which leads to a fairly efficient application of changes
For example, if we deployed the code below, Terraform/Tofu would already know to first create a resource with a virtual network, and only after that it would add the address pool to that network:
resource "opennebula_virtual_network" "ceph-virtnet" {
name = "ceph-intnet"
group = "oneadmin"
permissions = "600"
bridge = "vnet0"
gateway = "192.0.2.1"
dns = "192.0.2.1 8.8.8.8"
network_mask = "255.255.255.0"
}
resource "opennebula_virtual_network_address_range" "ceph" {
virtual_network_id = opennebula_virtual_network.ceph-virtnet.id
ar_type = "IP4"
size = 250
ip4 = "192.0.2.2"
}
Usually you don’t need to interfere with dependency building, but there are rare cases where you need to define explicit dependencies to manually determine the order of resource execution using the depends_on
meta-argument, for example:
resource "opennebula_virtual_network" "ceph-virtnet" {
name = "ceph-intnet"
group = "oneadmin"
permissions = "600"
bridge = "vnet0"
gateway = "192.0.2.1"
dns = "192.0.2.1 8.8.8.8"
network_mask = "255.255.255.0"
}
resource "opennebula_virtual_network_address_range" "ceph" {
virtual_network_id = opennebula_virtual_network.ceph-virtnet.id
ar_type = "IP4"
size = 250
ip4 = "192.0.2.2"
depends_on = [
opennebula_virtual_network.ceph-virtnet
]
}
Practice
If desired, the dependency graph can be printed using the graph
command:
tofu graph
terraform graph
The graph is output in a graph description language called DOT
, which can be easily converted to an image using, for example, the GraphvizOnline application, for this you just need to:
- Run
tofu graph
/terraform graph
- Copy the output of the command
- Paste the content into GraphvizOnline and view the image
Or you can use the dot
utility and generate an image with one command, execute it and view the graph.png
file:
tofu graph -type=plan | dot -Tpng >graph.png