AND and OR Conditional Operators in Terraform: Syntax and Implementation

Terraform supports standard && (AND) and || (OR) logical operators inside conditional expressions, enabling complex boolean logic with short-circuit evaluation.

Terraform's expression language, built on HCL 2, provides native boolean operators that integrate seamlessly with ternary conditionals. In the hashicorp/terraform repository, these operators are implemented as core language primitives within the vendored HCL library, allowing practitioners to construct sophisticated logic without nesting multiple conditional expressions. Understanding both the syntax and underlying implementation helps ensure efficient, readable infrastructure code.

How Logical Operators Work in Terraform Conditionals

Terraform conditional expressions follow the ternary pattern condition ? true_value : false_value. The condition argument accepts any expression that resolves to a boolean, including those composed using logical operators.

According to the source implementation in vendor/github.com/hashicorp/hcl/v2/hclsyntax/expression.go, Terraform evaluates these operators using short-circuit logic. When processing an && operation, evaluation stops immediately upon encountering the first false operand. Conversely, || operations halt at the first true operand, optimizing performance and preventing unnecessary evaluations.

Syntax and Usage Examples

Basic AND (&&) Operations

The && operator requires both operands to evaluate to true:

resource "aws_instance" "example" {
  count = var.enable && var.is_production ? 1 : 0
}

In this example, Terraform creates the instance only when var.enable is true and var.is_production is true.

Basic OR (||) Operations

The || operator evaluates to true when at least one operand is truthy:

locals {
  target_branch = var.branch == "dev" || var.branch == "staging"
}

Here, target_branch becomes true if the branch equals either "dev" or "staging".

Combining Operators with Parentheses

For complex decision trees, combine operators and use parentheses to control precedence:

locals {
  should_deploy = var.deploy_enabled && (var.branch == "dev" || var.manual_override)
}

output "environment" {
  value = var.is_production && var.use_high_availability ?
    "prod-ha" :
    var.is_production && !var.use_high_availability ?
    "prod-standard" :
    "non-prod"
}

The parentheses ensure the OR operation evaluates before the AND operation in the should_deploy calculation.

Implementation in the HCL Source Code

Under the hood, Terraform delegates expression parsing to the HCL 2 library vendored within the repository. The logical operators are defined in vendor/github.com/hashicorp/hcl/v2/hclsyntax/operator.go as part of the Op enumeration:

  • OpAnd maps to the && token
  • OpOr maps to the || token

The evaluation logic resides in vendor/github.com/hashicorp/hcl/v2/hclsyntax/expression.go, where the hclsyntax.Expression implementation constructs the abstract syntax tree (AST) and handles operand resolution. This architecture treats logical operators as first-class binary expressions, granting them the same type-checking, interpolation support, and error handling as other HCL constructs.

Summary

Frequently Asked Questions

What is the syntax for AND and OR operators in Terraform?

Terraform uses && for logical AND and || for logical OR. These symbols function identically to operators in languages like Python, JavaScript, or Go, allowing you to combine boolean expressions within ternary conditionals or standalone boolean contexts.

Does Terraform support short-circuit evaluation for logical operators?

Yes. As implemented in vendor/github.com/hashicorp/hcl/v2/hclsyntax/expression.go, the HCL parser evaluates && and || using short-circuit logic. This means && stops evaluating immediately upon finding a false operand, while || stops at the first true operand, improving performance and preventing potential errors in subsequent evaluations.

Can logical operators be used outside of ternary conditionals?

Absolutely. While commonly used inside ternary expressions (condition ? true_val : false_val), the && and || operators work anywhere HCL expects a boolean value, including resource count arguments, for expression filters, module for_each values, and variable validation conditions.

Where are the logical operators defined in the Terraform source code?

The token definitions reside in vendor/github.com/hashicorp/hcl/v2/hclsyntax/operator.go within the hclsyntax.Op enumeration, specifically as OpAnd for && and OpOr for ||. The runtime evaluation logic, including short-circuit implementation, is located in vendor/github.com/hashicorp/hcl/v2/hclsyntax/expression.go, which handles AST traversal and operand resolution during the evaluation phase.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s https://instagit.com/install.md

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client