Skip to content

empty list comparison does not work if the list contains an object #23562

@shanxops

Description

@shanxops

Terraform Version

Terraform v0.12.17

Terraform Configuration Files

Thanks @dpiddockcmp for a simpler example. #23562 (comment)

variable "object" {
  type    = list(object({ a = string }))
  default = [] 
}

output "equal" {
  value = var.object == [] ? "empty" : "not empty"
} 

output "not_equal" {
  value = var.object != [] ? "not empty" : "empty"
}

Expected Behavior

Outputs:
equal = empty
not_equal = empty

Actual Behavior

Outputs:
equal = not empty
not_equal = not empty

Steps to Reproduce

terraform init
terraform apply -auto-approve

Solution

variable "object" {
  type    = list(object({ a = string }))
  default = []
}

output "equal" {
  value = length(var.object) == 0 ? "empty" : "not empty"
}

output "not_equal" {
  value = length(var.object) != 0 ? "not empty" : "empty"
}

References

terraform-aws-modules/terraform-aws-eks#606 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions