Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ Terraform Series – Day 10

Published
β€’3 min read
πŸ“˜ 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

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)

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

🧠 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

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

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

πŸ”· Step 4: Testing the Conditional Expression

βœ… Case 1: Production Environment

env = "prod"

πŸ” Output Behavior

  • Condition β†’ true

  • Volume Size β†’ 20 GB

βœ” Instance will be created with larger storage (production-ready)

βœ… Case 2: Development Environment

env = "dev"

πŸ” Output Behavior

  • Condition β†’ false

  • Volume Size β†’ 10 GB

βœ” Instance will be created with default storage (cost-saving)

πŸ”· 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

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

πŸ“¬ Let's Stay Connected

Terraform

Part 2 of 11

πŸš€ Terraform Series – Automate Your Infrastructure Starting a complete **Terraform series** where I’ll cover everything from **basic to advanced level** with real-world practicals. In this series, you will learn: β€’ What is Infrastructure as Code (IaC) & why it matters β€’ Terraform fundamentals (providers, resources, state) β€’ Writing and managing Terraform configurations β€’ Variables, outputs & modules β€’ Remote state & state management β€’ Provisioning infrastructure on AWS β€’ Automation & real-world use cases β€’ Advanced concepts like workspaces, modules, and best practices 🎯 Goal: Help you automate infrastructure and become job-ready in DevOps. Perfect for **beginners, students, and DevOps learners** who want hands-on experience. Stay tuned and let’s build infrastructure the smart way βš‘πŸ’» #Terraform #DevOps #Cloud #AWS #InfrastructureAsCode #Automation

Up next

πŸ“˜ Terraform Series – Day 9

Meta Arguments: count vs for_each with Output πŸ“ Abstract In Terraform, managing multiple resources efficiently is a common requirement in real-world DevOps projects. While creating multiple instances