# 📘 Terraform Series – Day 10

### Conditional Expressions (Ternary Operator)

### 📝 Abstract

In real-world DevOps projects, infrastructure often needs to behave differently based on environments such as **development, staging, and production**. Instead of writing separate configurations, Terraform provides **conditional expressions (ternary operators)** to dynamically assign values.

This blog explains how to use conditional expressions in Terraform to make infrastructure more **flexible, reusable, and environment-aware**.

### 🎯 Objectives

After completing this blog, you will be able to:

*   Understand **conditional expressions in Terraform**
    
*   Use the **ternary operator syntax**
    
*   Apply conditions in real resources (EC2 example)
    
*   Configure infrastructure based on environment (`dev`, `prod`)
    
*   Test and verify conditional logic
    

> ### 🔷 Step 1: What is a Conditional Expression?

A **conditional expression** (also called a **ternary operator**) is used to assign values based on a condition.

🧠 Syntax

```shell
condition ? true_value : false_value
```

👉 Meaning

*   If condition is **true** → use `true_value`
    
*   If condition is **false** → use `false_value`
    

> ### 🔷 Step 2: Real Example (EC2 Root Volume)

```shell
root_block_device {
  volume_size = var.env == "prod" ? 20 : var.ec2_root_default_storage_size
  volume_type = "gp3"
}
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/6ef10499-cf5f-4563-a641-63ca3fa8b0d7.png align="center")

### 🧠 Logic Explained

*   If `env = "prod"` → volume size = **20 GB**
    
*   Else → volume size = **default value (10 GB)**
    

👉 This helps in **automatically adjusting infrastructure** based on environment

> ### 🔷 Step 3: Variables Used

```shell
variable "env" {
  default = "dev" or # "prod"
  type    = string
}

variable "ec2_root_default_storage_size" {
  default = 10
  type    = number
}
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/a2aba3bf-60a5-4c56-8fab-8219eb828042.png align="center")

> ### 🔷 Step 4: Testing the Conditional Expression

### ✅ Case 1: Production Environment

```shell
env = "prod"
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/61b9d164-e794-45c0-9c32-fc205de80981.png align="center")

### 🔍 Output Behavior

*   Condition → `true`
    
*   Volume Size → **20 GB**
    

✔ Instance will be created with **larger storage (production-ready)**

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/fc7bb608-5e48-4bb1-aafa-655be39812ec.png align="center")

### ✅ Case 2: Development Environment

```shell
env = "dev"
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/9e951d97-5aec-488d-8998-8e030e3d47f1.png align="center")

🔍 Output Behavior

*   Condition → `false`
    
*   Volume Size → **10 GB**
    

✔ Instance will be created with **default storage (cost-saving)**

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/5f0f3030-16d9-470a-9a04-b8f706f4bb1a.png align="center")

> ### 🔷 Step 5: Why Use Conditional Expressions?

Using conditional expressions helps in:

*   Reducing duplicate code
    
*   Managing multiple environments easily
    
*   Improving infrastructure flexibility
    
*   Writing clean and reusable Terraform code
    

> ### 🔷 Step 6: Best Practices

*   Use conditions for **environment-based configurations**
    
*   Keep values **simple and readable**
    
*   Avoid overly complex nested conditions
    
*   Combine with variables for better control
    

### 🚀 Conclusion

*   Conditional expressions allow **dynamic value assignment**
    
*   Syntax is simple: `condition ? true_value : false_value`
    
*   Useful for handling **prod vs dev differences**
    
*   Makes Terraform code **clean, scalable, and reusable**
    

### **👨‍💻 About the Author**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751797710818/123a7231-3dca-4273-ad68-7bd026f69b95.png?auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp align="center")

“A complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.”

### **📬 Let's Stay Connected**

*   📧 **Email**: [**gujjarapurv181@gmail.com**](mailto:gujjarapurv181@gmail.com)
    
*   🐙 **GitHub**: [**github.com/ApurvGujjar07**](http://github.com/ApurvGujjar07)
    
*   💼 **LinkedIn**: [**linkedin.com/in/apurv-gujjar**](http://linkedin.com/in/apurv-gujjar)
