Dependencies, implicit and explicit
Let references order your resources, and add depends_on only for the hidden dependency Terraform cannot see, like a policy a server needs at boot.
Dependencies, implicit and explicit
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A reference to another object's attribute creates an implicit dependency. It is the right way to express almost every ordering need.
- `depends_on` is for a dependency that exists in the real world but not in your arguments, such as an IAM policy a server needs at boot.
- `depends_on` takes a list of resources or modules, never attributes, and it is a blunt tool: it makes everything in the block wait.
- On a data source, `depends_on` defers the read until apply, so the values become unknown at plan time.
- A dependency cycle is an error. Break it by removing a reference or splitting one resource.
Two resources, one race
A server is launched with an instance profile that grants it access to a bucket. The role’s policy is attached by a separate resource. The instance’s arguments name the profile, which names the role, so Terraform knows to build those in order. It does not know that the server’s start-up script calls the bucket within seconds of boot, and that the policy attachment must be finished by then. Most days the attachment wins the race. One day it does not, the first boot fails, and the server comes up broken.
Terraform orders work with a graph, as you saw in the plan lesson. This lesson is about the two ways an edge gets into it.
Implicit: the reference is the edge
When one block’s argument refers to another’s attribute, Terraform adds an edge. In this example, written from the provider docs and not run:
resource "aws_subnet" "a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
}
aws_vpc.main.id supplies the value and creates the ordering in one step. The subnet waits for the VPC to exist, since the ID is not known until then. You never wrote “after”.
I ran a stand-in graph builder over a made-up network, which reads references and depends_on and prints the edges. It is my own code, and Terraform’s graph is richer:
aws_subnet.a implicit=['aws_vpc.main'] explicit=[]
aws_security_group.web implicit=['aws_vpc.main'] explicit=[]
aws_instance.web implicit=['aws_security_group.web', 'aws_subnet.a'] explicit=['aws_internet_gateway.gw']
The first two rows are references only. The instance has two implicit edges, from its subnet_id and its security group list, plus one it needed me to declare.
This is why Terraform code needs so few ordering statements. When you find yourself wanting to write “do A then B”, look for a value of A that B uses. Often there is one.
Explicit: depends_on
Sometimes the relationship is real but no value flows. The instance above needs the internet gateway to exist before it boots, because the boot script downloads packages, but none of its arguments refer to the gateway. You tell Terraform:
resource "aws_instance" "web" {
subnet_id = aws_subnet.a.id
vpc_security_group_ids = [aws_security_group.web.id]
depends_on = [aws_internet_gateway.gw]
}
depends_on takes a list of resources or modules, not attributes. In my graph output above, that edge shows as dashed.
It is blunt. The docs call it a last resort and warn it can make plans more conservative, replacing more than necessary, and they ask you to comment why it is needed. Prefer a reference when one is available, since a reference is both the order and the explanation. When you do write depends_on, leave a comment saying what runtime fact justifies it. The next person will want to delete it, and the comment is what stops them.
depends_on and data sources
Adding depends_on to a data source has a notable effect, as I understand the data source docs, which I did not re-read for this lesson: if what it depends on has changes pending, Terraform cannot read it during plan, so the read moves to apply and its values show as (known after apply). That makes sense, since the data may not exist until the dependency is built. The depends_on docs I did read say it works with a data block when that block does not directly reference the resource. It also means you cannot use those values where plan-time knowledge is needed, such as for_each keys. If a data source plan behaves oddly, look for a depends_on on it.
Cycles
If A refers to B and B refers to A, there is no valid order and Terraform stops with a cycle error. The classic cause is two AWS security groups that reference each other’s IDs in inline rules. Neither can be created first. The way out is to take the rules out of the groups, into separate rule resources that each reference both groups. The groups then have no dependency on each other, and the rules depend on both.
My locals stand-in from the earlier lesson reproduces the shape: a -> b -> a is rejected. The fix is always to break one of the edges, not to hide it.
Hidden dependencies you meet often
- IAM policy attachments that must complete before a service starts using the role.
- Network gateways and route table associations that must exist before an instance reaches the internet.
- A database that must accept connections before a migration runs, which is usually a sign to use a different tool for the migration.
- Anything coupled through a time delay.
depends_onguarantees creation order, not that the thing is ready. A shorttime_sleepresource from the time provider is the pragmatic answer, and it gets its own lesson in the providers group.
About the video
The embed is Matthew Sanabria’s “Terraform Shorts: Implicit and Explicit Dependencies”, August 2023, all four minutes. The description says it covers how Terraform builds the graph implicitly and how to add explicit dependencies. It is short and does not show cycles or the depends_on effect on data sources, which come from the lesson text. I judged the content from the description. English was judged from title, channel and English auto-captions.
Where this fits
That finishes the language group. You can declare, compute, repeat, template, read and order. The next group takes on the thing that actually talks to clouds: providers and the registry.
Check yourself
Your instance needs an IAM policy attached before it starts, but nothing in its arguments mentions the policy attachment. What do you add?
`depends_on = [aws_iam_role_policy_attachment.app]` on the instance. The relationship exists at runtime, not in a value, so Terraform needs to be told.
Why prefer a reference over `depends_on` when both would work?
A reference orders only what needs the value and documents why. `depends_on` makes the whole block wait, can force wider updates, and says nothing about the reason.
What happens to a data source that has `depends_on` pointing at a resource that is changing?
Terraform defers reading it until apply, so its values are unknown at plan time. That can stop you using it in `count` or `for_each`.
Terraform reports a cycle between two security groups that reference each other's IDs in inline rules. What is a way out?
Use separate rule resources that reference both groups, so the groups exist first and the rules depend on both. The cycle came from rules being inside the group.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.