Skip to content

The "count" value depends on resource attributes that cannot be determined until apply #28962

@ghost

Description

Terraform Version

1.0.0

main.tf

resource "random_id" "random_label" {
  byte_length = 2
}

module "a" {
  source    = "./child"
  name      = "a"
  //  label = null
  label = random_id.random_label.hex
}


output "main-test-out" {
  value = random_id.random_label.id
}

###child/main.tf

variable "name" {
  type = string
}

variable "label" {
  type = string
  default = null
}

resource "null_resource" "x" {
  count = var.label == null ? 1 : 0
  //depends_on = [var.label,]
}

output "ids" {
  value = null_resource.x.*.id
}

Crash Output

Error: Invalid count argument

on child/main.tf line 11, in resource "null_resource" "x":
11: count = var.label == null ? 1 : 0

The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the count depends on.

Explaination

I built a minimal example to show the problem I have. Is there any way to give a random label to a module, which uses this to decide whether to create a resource or not (using count)?

Expected Behavior

I expect that random_label gets created first and then count is applied.

Actual Behavior

Apparantly, count is applied first. Since random_label is needed to decide whether a resource is to be created or not, an error is thrown.

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. terraform init
  2. terraform apply

References

I believe this is the same issue as #26078

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnewnew issue not yet triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions