DEV Community

Brian Mengo
Brian Mengo

Posted on

Understanding Terraform Expressions: Conditional, Dynamic Blocks, and Splat Expressions

Terraform Expressions Overview
Terraform expressions offer concise, reusable ways to derive values or generate multiple resource configurations in Terraform scripts. They help prevent redundant code and enhance flexibility prior to mastering Terraform functions.

Expressions are not functions but act similarly to functions by avoiding redundant code.
The three main types discussed:

- Conditional Expression: A one-liner true/false condition akin to ternary operators.
- Dynamic Block: Generates nested resource blocks iteratively based on variable data.
- Splat Expression: Retrieves lists of resource attributes efficiently using the star operator (*).

1. Conditional Expressions
Conditional expressions are Terraform’s inline if-else statements to select values based on Boolean conditions without rewriting blocks.

Structure:

\text{condition} \quad ? \quad \text{true_value} \quad : \quad \text{false_value}

Example usage: dynamically select AWS instance types based on environment variable var.environment.

Example

instance_type = var.environment == "dev" ? "t2.micro" : "t3.micro"
Enter fullscreen mode Exit fullscreen mode
  • If environment is "dev", the instance type is t2.micro (smaller, cheaper).
  • For any other environment, it uses t3.micro (larger, suitable for production).

2. Dynamic Blocks
Dynamic blocks facilitate creating multiple nested resource blocks based on the contents of lists or maps without manually repeating blocks.

Concept

  • Useful for resources with nested blocks such as AWS Security Group ingress or egress rules.
  • Iterate over complex data structures like a list of objects.
  • Avoid duplication by generating multiple blocks dynamically through a loop (for_each).

Variable Definition
Defined a variable ingress_rules of type list(object(...)) with keys and types:

**Key**       **Type**  **Description**
from_port   number  Starting port of rule
to_port         number  Ending port of rule
protocol    string  Protocol (e.g., "tcp", "udp")
cidr_blocks  list of string Allowed CIDRs
description string  Rule description (optional)
Enter fullscreen mode Exit fullscreen mode

Multiple ingress rules are stored as a list of such objects.

Applying Dynamic Blocks
In the AWS security group resource, replaced static ingress blocks with:

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
  }
}

Enter fullscreen mode Exit fullscreen mode

3. Splat Expressions
Splat expressions simplify extracting lists of resource attribute values when a resource is created multiple times with count or for_each.

Use case
Aggregate resource attributes (e.g., instance IDs) into a single list variable or output.
Access across all instances without writing manual loops or multiple references.

Syntax

resource_type.resource_name[*].attribute
Enter fullscreen mode Exit fullscreen mode
  • The star operator * iterates all instances created via count or for_each.
  • Returns a list of values of the specified attribute from all instances.

Example
To collect all AWS instance IDs into a list:

locals {
  all_instance_ids = aws_instance.example[*].id
}

output "instances" {
  value = local.all_instance_ids
}

Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Terraform expressions enhance reusability and dynamic configuration without full functions.
  • Conditional expressions reduce hardcoded attributes based on environment or other variables.
  • Dynamic blocks efficiently handle multiple nested blocks like ingress rules driven by variable data.
  • Splat expressions aggregate attribute lists from multiple resources simply via the star operator (*).
  • Following official Terraform documentation closely is critical to avoid syntax and typing errors.

Top comments (0)