0Pricing
DevOps Bootcamp · Lesson

Conditional Expressions and Splats

Implement conditional logic in your configurations using ternary operators and understand splat expressions for transforming lists of objects.

Conditional Expressions and Splats is a free DevOps Bootcamp lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Dynamic Logic in Terraform

Terraform isn't just about static declarations! To make your infrastructure configurations smarter and more adaptable, we use dynamic logic.

This lesson introduces two powerful tools: conditional expressions and splat expressions. They help your infrastructure adapt to different situations and efficiently transform data.

Conditional Expressions: The Basics

Conditional expressions, often called ternary operators, allow you to choose one of two values based on whether a condition is true or false. It's like a simple 'if-else' statement for values.

  • Syntax: condition ? value_if_true : value_if_false
  • They are great for making configurations flexible and reusable, adapting to different environments or settings.

Conditional: Simple Value Example

Let's see how a conditional expression can dynamically set a local variable's value based on an input variable. Try changing default = "dev" to default = "prod" and re-running.

variable "env" {
  description = "Deployment environment (dev or prod)"
  type        = string
  default     = "dev"
}

locals {
  instance_type = var.env == "prod" ? "t3.medium" : "t3.micro"
}

output "chosen_instance_type" {
  value = local.instance_type
  description = "The instance type selected based on environment."
}

Conditional: Resource Attribute Example

You can also use conditionals to control resource attributes, even whether a resource is created at all! Here, we conditionally create an S3 bucket using the count meta-argument.

variable "create_bucket" {
  description = "Whether to create an S3 bucket"
  type        = bool
  default     = true
}

resource "aws_s3_bucket" "my_bucket" {
  # If create_bucket is true, count is 1 (create one bucket).
  # If create_bucket is false, count is 0 (create no buckets).
  count  = var.create_bucket ? 1 : 0
  bucket = "coddykit-unique-bucket-12345" # IMPORTANT: Replace with a globally unique name!
  acl    = "private"
}

output "bucket_id" {
  value = var.create_bucket ? aws_s3_bucket.my_bucket[0].id : "No bucket created"
  description = "The ID of the created S3 bucket, or a message if not created."
}

Understanding Splat Expressions

Splat expressions ([*]) are used to transform a list of objects into a list of specific attributes from those objects. It's a powerful shortcut for extracting data from multiple items at once.

  • Syntax: LIST_OF_OBJECTS[*].ATTRIBUTE
  • They simplify working with collections, such as multiple resources created with count or complex list variables.

Splat: Resource Outputs Example

Imagine you create multiple EC2 instances. How do you get a list of all their public IPs easily? Splat expressions are perfect for this! Note: You'll need a valid AMI for your AWS region.

resource "aws_instance" "web" {
  count         = 2 # Creates two instances
  ami           = "ami-0abcdef1234567890" # Replace with a valid AMI ID for your region
  instance_type = "t2.micro"
  tags = {
    Name = "web-instance-${count.index}"
  }
}

output "web_instance_public_ips" {
  value = aws_instance.web[*].public_ip
  description = "A list of public IP addresses for all web instances."
}

Splat: Variable Lists Example

Splat expressions aren't just for resources; they work great with complex variables too. Here, we have a list of user objects and want to extract only their email addresses.

variable "users" {
  description = "List of user objects, each with name, email, and role."
  type = list(object({
    name  = string
    email = string
    role  = string
  }))
  default = [
    { name = "Alice", email = "alice@example.com", role = "admin" },
    { name = "Bob", email = "bob@example.com", role = "viewer" },
    { name = "Charlie", email = "charlie@example.com", role = "editor" }
  ]
}

output "user_emails" {
  value = var.users[*].email
  description = "A list containing only the email addresses of the users."
}

Combining Conditionals & Splats

Conditional and splat expressions can be combined for even more advanced and concise logic. For example, you might conditionally select a list of resources and then use a splat expression on the chosen list.

  • This allows for highly adaptive configurations that respond to complex scenarios.
  • Example: (var.env == "prod" ? aws_instance.prod : aws_instance.dev)[*].id could fetch IDs from different instance sets based on the environment.

Best Practices for Dynamic Logic

While powerful, dynamic logic can sometimes make configurations harder to understand if not used carefully. Here are some tips:

  • Keep it Readable: Break down complex expressions into smaller, more manageable parts.
  • Use Locals: Store complex conditional or splat expressions in locals blocks to improve readability and prevent repetition.
  • Test Thoroughly: Dynamic logic can lead to unexpected behavior. Always run terraform plan and carefully review the proposed changes.
  • Document: Add comments to explain complex logic, especially when combining conditionals and splats.

Quick Check

Consider the following Terraform configuration:

variable "server_names" {
  type    = list(string)
  default = ["web-01", "db-01", "cache-01"]
}

output "filtered_names" {
  value = var.server_names[*]
}

Recap & Next Steps

Great job! You've learned how to make your Terraform configurations much more dynamic and flexible:

  • We explored conditional expressions (ternary operator) to make values dynamic based on specific conditions.
  • We mastered splat expressions ([*]) to efficiently extract lists of attributes from collections of objects or resources.

These tools are fundamental for building scalable, reusable, and adaptive infrastructure as code. Keep practicing to integrate them into your daily Terraform workflows!

Frequently asked questions

Is the “Conditional Expressions and Splats” lesson free?

Yes — the full text of “Conditional Expressions and Splats” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Conditional Expressions and Splats”?

Implement conditional logic in your configurations using ternary operators and understand splat expressions for transforming lists of objects. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Conditional Expressions and Splats” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Using Loops with `for_each` and `count`
  2. Terraform Built-in Functions
  3. Conditional Expressions and Splats
  4. Dynamic Blocks for Nested Configuration
← Back to DevOps Bootcamp