FG
☁️ Cloud & DevOps

Support use cases with conditional logic

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

It's been important from the beginning that Terraform's configuration language is declarative, which has meant that the core team has intentionally avoided adding flow-control statements like conditionals and loops to the language. But in the real world, there are still plenty of perfectly reasonable scenarios that are difficult to express in the current version of Terraform without copious amounts of duplication because of the lack of conditionals. We'd like Terraform to support these use cases one way or another. I'm opening this issue to collect some real-world example where, as a config author, it seems like an `if` statement would really make things easier. Using these examples, we'll play around with different ideas to improve the tools Terraform provides to the config author in these scenarios. So please feel free to chime in with some specific examples - ideally with with blocks of Terraform configuration included. If you've got ideas for syntax or config language features that could form a solution, those are welcome here too. (No need to respond with just "+1" / :+1: on this thread, since it's an issue we're already aware is important.)

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Conditional Logic Support in Terraform Configurations

Medium Risk

Terraform's declarative nature currently lacks support for flow-control statements like conditionals and loops, which leads to excessive code duplication and complexity in configurations. This limitation makes it challenging to handle scenarios where resource creation or configuration should depend on certain conditions.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define Conditional Logic Syntax

    Propose a new syntax for conditional logic in Terraform configurations, such as using 'if' statements to control resource creation based on variable values.

    hcl
    resource "aws_instance" "example" {
      count = var.enable_instance ? 1 : 0
      ami = var.ami_id
      instance_type = var.instance_type
    }
  2. 2

    Create a Feature Proposal Document

    Draft a detailed document outlining the proposed syntax, use cases, and examples of how conditional logic can simplify Terraform configurations. Include feedback from the community to strengthen the proposal.

  3. 3

    Implement a Prototype

    Develop a prototype of the conditional logic feature in a separate branch of the Terraform codebase. This prototype should allow users to test the new syntax and provide feedback on its usability and functionality.

  4. 4

    Conduct User Testing

    Engage with a group of Terraform users to test the prototype and gather feedback. Focus on real-world scenarios where conditional logic would be beneficial, and refine the implementation based on user input.

  5. 5

    Finalize and Merge the Feature

    After incorporating user feedback and making necessary adjustments, finalize the implementation and prepare it for merging into the main Terraform codebase. Ensure comprehensive documentation is included.

Validation

To confirm the fix worked, create a sample Terraform configuration using the new conditional logic syntax and apply it. Verify that resources are created or skipped based on the defined conditions without code duplication.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

terraformiacawsenhancementcore