Skip to content

Direct support for conditional single-instance resources #1306

@staizen-stephen

Description

@staizen-stephen

OpenTofu Version

None. Currently using Hashicorp Terraform.

Use Cases

We often use count = var.xxxx ? 1 : 0 in various flavours but with the same purpose of determining to create or not create a single resource.

While this is currently supported it means that resources require an indexed reference, and this also means that if you later change something to be conditional then you have a lot of manual state manipulation required.

I'd like to see this natively supported as it would simplify a lot of code.

Attempted Solutions

Workarounds are using count = ... expressions.

Proposal

Add an additional meta-data atttribute opentofu-options and a sub-attribute resource-enabled, for example:

resource "aws_key_pair" "my-key-pair" {
  ...
  opentofu-options {
    resource-enabled = var.create-key-pair
  }
}

When a resource is not enabled (known at plan time), then any reference to it in the graph becomes invalid and this would prevent a bunch of munging such as coalesce(aws-key_pair.my-key-pair.*.name, "") or equivalent.

References

Sub-issues

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestneeds-rfcThis issue needs an RFC prior to being accepted or, if it's accepted, prior to being implemented.pending-decisionThis issue has not been accepted for implementation nor rejected. It's still open to discussion.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions