Resource
Manages a real object and has an address such as aws_vpc.main.
Chapter 3 of 24
Configuration order is for people; references define the graph the CLI can safely execute.
depends_on is justified.count or for_each with address stability in mind.Manages a real object and has an address such as aws_vpc.main.
Reads an existing object. It is still a dependency and can fail or drift.
Locals name repeated expressions; outputs expose selected graph results.
Generates repeated provider nested arguments; it does not replace resource-level for_each.
resource "aws_subnet" "app" { vpc_id = aws_vpc.main.id } # implicit edge
resource "aws_route_table_association" "app" { depends_on = [aws_route.app] }
resource "aws_security_group" "rule" {
for_each = var.rules
dynamic "ingress" {
for_each = each.value.ingress
content { from_port = ingress.value.from_port }
}
}count uses numeric addresses such as [0]; inserting an item can retarget them. Prefer for_each with durable names when objects have identity.
depends_on only for an ordering dependency that has no value to reference.This lab uses only the built-in terraform_data resource. Later, the same edges will connect VPCs, subnets, route tables, and IAM objects, where a missing edge can produce a race or invalid API call.
Open labs/03-graph/main.tf. One resource implicitly consumes the network output, a second intentionally waits for all keyed subnets, and a separate count resource makes the address difference visible.
cd learn-terraform/labs/03-graph
terraform init -backend=false
terraform graph
terraform plancd learn-terraform/labs/03-graph
tofu init -backend=false
tofu graph
tofu planSave the DOT graph if Graphviz is available: tofu graph | dot -Tsvg > graph.svg.
Expect six resources: network, two keyed subnets, two numbered replicas, and completion. In the graph, trace subnet to network, then graph_complete to the subnet collection.
| Signal | Review question |
|---|---|
subnet["app-a"] | Does the key match durable object identity? |
replica[0] | Would insertion reorder this address? |
depends_on | Is there truly no attribute reference that could encode this edge? |
Do not add a back-reference from network to a subnet. Two nodes that require each other produce a cycle error; remove the artificial edge and model a separate attachment resource if the API requires a second phase.
terraform graph or tofu graph.Stop at plan. If you apply while exploring, run terraform destroy or tofu destroy with the same CLI. No AWS object is involved.
An attribute reference, such as a subnet consuming a VPC ID.
depends_on appropriate?For a real ordering dependency that cannot be represented by an attribute reference.
for_each for named subnets?Named addresses remain associated with the same object when another key is added.
Terraform and OpenTofu schedule a dependency graph, not a script. Stable addresses and explicit data flow make a plan easier to trust.