I want to conditionally create a resource based on a value that is not available at the time of terraform apply
. For example:
resource "example" "a" {
count = var.name == "john" ? 1 : 0
}
Where var.name
is a value that is generated from the random
provider. When I do a terraform apply
I encounter the following error:
Error: Invalid count argument
│
│ on modules\rds-config\main.tf line 30, in resource "aws_rds_cluster" "aurora_mysql_cluster":
│ 30: count = var.name== "john" ? 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.
The error tells me to apply the randomization resource first that is creating var.name
. I looked into the documentation for this (found here) and was concerned by the statement Targeting individual resources can be useful for troubleshooting errors, but should not be part of your normal workflow.
With all that said, is there a way to still apply all resources within one terraform apply
while conditionally creating certain resources depending on the evaluation of some other resource that is not available at the time of terraform apply
? Any input is greatly appreciated!