How to Write If Else Elsif Conditional Statements Using Terraform If Else Syntax

Terraform implements conditional logic through a ternary expression (condition ? true_result : false_result) rather than dedicated if/else blocks, and you can nest these expressions to create elsif chains.

Terraform's configuration language (HCL) does not provide a traditional if … else … elsif control flow structure found in imperative programming languages. Instead, the hashicorp/terraform repository implements conditional logic via expressions parsed in internal/hclsyntax/expr_if.go and evaluated during the planning phase. Understanding this terraform if else syntax is essential for writing dynamic, environment-aware infrastructure configurations.

Understanding Terraform's Conditional Expression Syntax

The core conditional construct in Terraform follows the ternary operator pattern:

condition ? true_result : false_result

According to the source code in [internal/hclsyntax/expr_if.go](https://github.com/hashicorp/terraform/blob/main/internal/hclsyntax/expr_if.go), Terraform defines a ConditionalExpr AST node that represents this operation. The evaluator processes these nodes during both planning and applying phases, as implemented in [internal/hcl/eval.go](https://github.com/hashicorp/terraform/blob/main/internal/hcl/eval.go).

  • condition: Any expression evaluating to a boolean (true or false)
  • true_result: The value returned when the condition evaluates to true
  • false_result: The value returned when the condition evaluates to false

Writing Basic If-Else Logic

For simple binary decisions, use a single conditional expression. This pattern selects between two values based on a boolean condition:

resource "aws_instance" "example" {
  ami           = var.region == "us-east-1" ? "ami-0abcd1234ef56789a" : "ami-0fedcba9876543210"
  instance_type = "t2.micro"
}

In this example, the ami attribute uses terraform if else syntax to choose between two Amazon Machine Images based on the region variable. When the condition evaluates to false, Terraform assigns the alternative value immediately following the colon.

Handling Multiple Branches (If-Elsif-Else)

To implement elsif logic (multiple mutually exclusive conditions), nest conditional expressions within the false_result position:

variable "env" {
  type = string
}

resource "aws_instance" "web" {
  instance_type = var.env == "prod"   ? "t2.large" :
                  var.env == "stage"  ? "t2.medium" :
                                        "t2.small"
  ami = "ami-0abcd1234ef56789a"
}

This chained pattern creates three distinct branches:

  1. If var.env == "prod", return "t2.large"
  2. Else if var.env == "stage", return "t2.medium"
  3. Else return "t2.small"

The Terraform parser handles these nested ConditionalExpr nodes recursively, allowing theoretically unlimited condition chaining, though readability typically limits practical usage to three or four branches.

Working with Complex Values and Objects

The terraform if else syntax accepts any expression type in both result positions, including objects, lists, or maps:

locals {
  server_config = var.env == "prod" ? {
    use_ssl      = true
    port         = 443
    instance_type = "t2.large"
  } : {
    use_ssl      = false
    port         = 80
    instance_type = "t2.small"
  }
}

This pattern proves particularly useful for conditionally merging configuration objects or selecting between entirely different resource parameter sets.

Using Null for Conditional Resource Attributes

When you need optional behavior rather than alternative values, use null in the false position:

resource "aws_instance" "conditional_ebs" {
  ami           = "ami-0abcd1234ef56789a"
  instance_type = "t2.micro"

  # Only attach volume in production

  ebs_block_device {
    device_name = var.env == "prod" ? "/dev/sdh" : null
    volume_size = var.env == "prod" ? 100 : null
  }
}

Setting an attribute to null effectively omits it from the final configuration, allowing conditional resource inclusion without complex branching logic.

Summary

  • Terraform implements conditionals through ternary expressions (condition ? true : false) defined in internal/hclsyntax/expr_if.go, not through if/else blocks
  • Create elsif logic by nesting conditional expressions in the false_result position
  • The evaluator processes these expressions during both planning and applying phases
  • Complex objects, lists, and maps work as valid result values in conditional expressions
  • Use null as a result value to conditionally exclude attributes entirely

Frequently Asked Questions

Does Terraform support traditional if/else blocks?

No. Terraform's HCL language does not provide imperative if/else control structures. Instead, the implementation in internal/hclsyntax/expr_if.go provides the ternary ConditionalExpr node that evaluates conditions during the planning phase. This functional approach ensures declarative configuration while still supporting dynamic logic.

How do I handle more than three conditions in Terraform?

Nest additional conditional expressions in the final false_result position of the previous condition. While the syntax supports unlimited nesting, consider using a lookup() function or map variable for readability when exceeding three or four branches. Each nested ternary creates another evaluation branch processed by the expression evaluator in internal/hcl/eval.go.

Can I use null in conditional expressions?

Yes. Using null as either the true_result or false_result allows you to conditionally omit attributes or entire blocks. When Terraform evaluates the conditional and encounters null, it treats the attribute as unset, which is particularly useful for optional resource parameters that should only exist under specific conditions.

Where is the conditional logic evaluated in Terraform's codebase?

The conditional expression parsing occurs in [internal/hclsyntax/expr_if.go](https://github.com/hashicorp/terraform/blob/main/internal/hclsyntax/expr_if.go), which defines the ConditionalExpr AST structure. The actual evaluation of these expressions happens in [internal/hcl/eval.go](https://github.com/hashicorp/terraform/blob/main/internal/hcl/eval.go), where Terraform evaluates both branches during planning to preserve unknown values and ensure consistent state calculation.

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