Skip to content

The "count" value depends on resource attributes that cannot be determined until apply, but resource attributes are already applied #26078

@dimisjim

Description

@dimisjim

Terraform Version

0.13.1

Terraform Configuration Files

#################
# at root level #
#################

module "profileapi" {
  source                 = "./modules/s3_bucket"
  bucket_name            = "1plusx-profileapi"
  enable_versioning      = true
  retention_days         = 7
  destination_bucket_arn = module.profileapi-dub.bucket_arn

  providers = {
    aws = aws.frankfurt
  }
}

module "profileapi-dub" {
  source            = "./modules/s3_bucket"
  bucket_name       = "1plusx-profileapi-dub"
  enable_versioning = true
  retention_days    = 7

  providers = {
    aws = aws.dublin
  }
}

output "test" {
  value = module.profileapi-dub.bucket_arn
}

###########################
# inside s3_bucket module #
###########################
locals {
  enable_replication = signum(length(var.destination_bucket_arn)) == 1 ? true : false
  replication_cnt    = signum(length(var.destination_bucket_arn))
}

resource "aws_s3_bucket" "logged_retention_crrd_bucket" {
  count         = var.enable_logging && var.enable_expiration && local.enable_replication && false == local.enable_encryption ? 1 : 0
...
}

resource "aws_s3_bucket" "logged_retention_bucket" {
  count         = var.enable_logging && var.enable_expiration && var.encryption_algorithm == "" && false == local.enable_replication ? 1 : 0
...
}

resource "aws_s3_bucket" "retention_bucket" {
  count         = false == var.enable_logging && var.enable_expiration && var.encryption_algorithm == "" && false == local.enable_replication ? 1 : 0
...
}

resource "aws_s3_bucket" "logged_bucket" {
  count         = var.enable_logging && false == var.enable_expiration && var.encryption_algorithm == "" && false == local.enable_replication ? 1 : 0
...
}

Debug Output

Trace outputs were too large for gist, so used gdrive instead

Snippet of the errors:

Error: Invalid count argument

  on modules/s3_bucket/main.tf line 434, in resource "aws_s3_bucket" "logged_retention_crrd_bucket":
 434:   count         = var.enable_logging && var.enable_expiration && local.enable_replication && false == local.enable_encryption ? 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.

Expected Behavior

As it was in 0.12, the module output should be computed and provided to the root level where it is referenced as one of another's module's inputs, without having to explicitly define an output value with it. Commands like state show correctly output the state that the other module presumes that has never been applied.

Actual Behavior

tf 0.13 thinks that module has never been applied, and therefore its output is not available to another module which uses it as an input.

Steps to Reproduce

  1. Define the outputs and apply to make them available
  2. Run tf plan, which works fine
  3. Remove the outputs, and apply again in order to remove them from the state.
  4. Plan again, now you get "Error: Invalid count argument"

Additional Context

Nothing special in particular.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugexplaineda Terraform Core team member has described the root cause of this issue in code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions