Skip to content

Empty destroy fails on conditional modules #31838

@jta

Description

@jta

Terraform Version

Terraform v1.3.0
on darwin_amd64
+ provider registry.terraform.io/hashicorp/null v3.1.1

Terraform Configuration Files

main.tf:

locals {
  enable = true
  value  = local.enable ? module.example[0].thing : null
}

module "example" {
  count  = local.enable ? 1 : 0
  source = "./example"
}

The contents of .example/main.tf:

resource "null_resource" "thing" {
}

output "thing" {
  value = null_resource.thing
}

Debug Output

https://gist.github.com/jta/4b9bd020354d470c6d1f006b7baf7341

Expected Behavior

Destroying where there is no state should be a no-op.

Actual Behavior

Terraform can apply and destroy the provided config correctly. However, issuing a destroy when the state is empty triggers an error:

Error: Invalid index

  on main.tf line 3, in locals:
   3:   value  = local.enable ? module.example[0].thing : null
    ├────────────────
    │ module.example is empty tuple

The given key does not identify an element in this collection value: the collection has no elements.

Steps to Reproduce

  1. terraform init
  2. terraform destroy

Additional Context

This is a regression introduced by v1.3.0.

References

No response

Metadata

Metadata

Assignees

Labels

bugconfirmeda Terraform Core team member has reproduced this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions