Skip to content

Boolean operators should be capable of converting unknown values to known #31078

@Nuru

Description

@Nuru

Boolean operators && and || should be capable of converting unknown values to known values based on standard boolean logic.

&& true false unknown
true true false unknown
false false false false
unknown unknown false unknown
|| true false unknown
true true true true
false true false unknown
unknown true unknown unknown

Terraform Version

1.1.7

Terraform Configuration Files

This is obviously contrived to show the issue, but in practice this sort of thing allows certain configurations to tolerate certain unknowns. For example, a configuration could require either a list of AWS Availability Zones (AZs) or the number of zones to use, with the final list of AZs being either the configured list or the list from a data source, and the final count being either the provided count or the size of the configured list of AZs. Currently this has to be carefully managed with the tertiary operator ? : but it should be able to be managed with straightforward boolean arithmetic.

variable "known" {
  type = bool
  default = false
}

resource "null_resource" "unknown" {
}

locals {
  computed = var.known && (length(null_resource.unknown.id) == 3)
  # The following statement avoid the problem:
  # computed = var.known ? length(null_resource.unknown.id) == 3 : false
}

resource "null_resource" "count" {
  count = local.computed ? 1 : 0
}

Expected Behavior

count should be known at plan time.

Actual Behavior

Error: Invalid count argument

Steps to Reproduce

  1. terraform init
  2. terraform apply

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions