Handling linear dependencies when count of resource is zero

Hello, I wanted to understand how linear dependencies are handled. In my example,

resource "example" "example1" {
//some code here
count = 1

resource "example" "example2" {
count = local.if_needed ? 1: 0

depends_on = [example.example1]

resource "new_resource" "example3" {
depends_on = [example.example2]

Now suppose when the count of example.example2 is 0, I want new_resource.example3 to be dependent on example.example1. As example.example2 is dependent on example.example1, will example.example3 now depend on example.example1 when the configuration is evaluated or should I change my implementation to

resource "new_resource" "example3" {
depends_on = [example.example1, example.example2]

Hi @chiraniamanav15,

Dependencies are between resource blocks in the configuration, not between the dynamic instances of those resources. A dependency on a resource with count = 0 is valid, because the resource block still exists even though it declares zero instances.

Hi, thanks for the reply. Does that mean that for the first code block, new_resource.example3 will be created only after example.example1 if the count of example2 is zero?

Yes. Specifically, Terraform will wait until example1 is complete before even calculating how many instances of example2 there are. Terraform won’t know there are zero instances until it reaches that resource during evaluation. Only after it’s decided how many instances of example2 there are and evaluated each of them (which might be zero of them) will it move on to evaluate example3.

I tried this particular code -

resource "null_resource" "test1" {

resource "null_resource" "test2" {
  depends_on = [null_resource.test1]

resource "null_resource" "test3" {
  count =  0
  depends_on = [null_resource.test2]

resource "null_resource" "test4" {
  depends_on = [null_resource.test3]

resource "null_resource" "test5" {
  depends_on = [null_resource.test2, null_resource.test3]

So, here test4 should’ve been created after test2 is created but the output I get is -

null_resource.test4: Creating...
null_resource.test1: Creating...
null_resource.test4: Creation complete after 0s [id=1547977163223384883]
null_resource.test1: Creation complete after 0s [id=5878378976281927211]
null_resource.test2: Creating...
null_resource.test2: Creation complete after 0s [id=955801719549354720]
null_resource.test5: Creating...
null_resource.test5: Creation complete after 0s [id=4426660689960026593]

Am i missing something here?

Hi @chiraniamanav15,

I think that might be considered a bug in the dependency handling, though I’m not sure how it should be resolved at the moment.

What is happening is that test4 depends on the test3 configuration being evaluated, but there are no instances planned to go long with that evaluation. Because there are no instances there is nothing to connect the test4 instances with the test2 instances. The test3 config (the config is represented by (expand) here) does depend on test2 (expand), but that does not depend on the test2 instances because the dependency travels in the inverse direction.

This results in the configuration evaluation happening in the correct order, but the actual resource instances may be applied in a different order. This doesn’t normally matter because once once the configuration is complete most resources can be applied without a problem, but depends_on is also used to represent dependencies outside of terraform which are not visible via the instance configuration.

I’ll file an issue to go along with the explanation so we can track it.

1 Like

Oh okay, got it! Thanks @jbardin for the explanation.