DEV Community

Cover image for ->> Day-10 Terraform Conditional Expression, Splat Expression and Dynamic Block
Amit Kushwaha
Amit Kushwaha

Posted on

->> Day-10 Terraform Conditional Expression, Splat Expression and Dynamic Block

A Quick Intro Before We Jump In

Terraform is amazing because it lets us treat our cloud infrastructure like normal code — reusable, predictable, repeatable. But some of its features can feel mysterious until someone explains them in plain English.

Today, we’ll look at three concepts that make your code cleaner and your life way easier:

  • Conditional Expressions
  • Splat Expressions
  • Dynamic Blocks

Quick Brief

  • Conditional Expressions → Terraform’s mini if-else for picking values.
  • Splat Expressions → Grab a list of attributes from multiple resources.
  • Dynamic Blocks → Auto-generate repeated nested blocks without manually typing them.

1. Conditional Expressions

Ever wanted Terraform to decide something automatically?
Like choosing a bigger EC2 instance only in production?

Conditional expressions make that happen.

Example

resource "aws_instance" "conditional_example" {
  ami           = data.aws_ami.amazon_linux.id
  instance_type = var.environment == "prod" ? "t3.large" : "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

This is basically Terraform saying:

  • If env = prod, go big
  • Else, chill with a smaller instance Super handy for environment-based configs.


2. Splat Expressions

Okay, imagine you created three EC2 instances and now you’re like,
“Terraform, give me all their IDs, please.”

Instead of manually listing them, you can splat them.
(Sounds magical, kinda is.)

Example

resource "aws_instance" "splat_example" {
  count = var.instance_count

  ami           = data.aws_ami.amazon_linux.id
  instance_type = "t2.micro"

  tags = {
    Name = "instance-${count.index + 1}"
  }
}

# Use splat expressions to extract values from all instances
locals {
  # Get all instance IDs in one line using [*]
  all_instance_ids = aws_instance.splat_example[*].id

  # Get all private IPs using [*]
  all_private_ips = aws_instance.splat_example[*].private_ip
}

Enter fullscreen mode Exit fullscreen mode


3. Dynamic Blocks

Now this one is powerful.
Let’s say you want multiple ingress rules in a security group.
You could copy-paste the block three times, but…
why suffer?

Dynamic blocks let Terraform generate repeated nested blocks for you.

Example:

resource "aws_security_group" "dynamic_sg" {
  name        = "dynamic-sg-${var.environment}"
  description = "Security group with dynamic rules"

  # Dynamic block creates multiple ingress rules from a list
  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      from_port   = ingress.value.from_port
      to_port     = ingress.value.to_port
      protocol    = ingress.value.protocol
      cidr_blocks = ingress.value.cidr_blocks
      description = ingress.value.description
    }
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "dynamic-sg-${var.environment}"
  }
}
Enter fullscreen mode Exit fullscreen mode

If var.ports = [22, 80, 443], Terraform will automatically create three ingress rules.

Conclusion

Conditional expressions, splat expressions, and dynamic blocks are small concepts with HUGE impact. Once you start using them:

  • Your Terraform code becomes cleaner
  • You avoid repetition
  • Your infrastructure adapts automatically

Reference

>> Connect With Me

If you enjoyed this post or want to follow my #30DaysOfAWSTerraformChallenge journey, feel free to connect with me here:

💼 LinkedIn: Amit Kushwaha

🐙 GitHub: Amit Kushwaha

📝 Hashnode / Amit Kushwaha

🐦 Twitter/X: Amit Kushwaha

Top comments (0)