AWS with Terraform (Day 10)

Conditional Expressions, Dynamic Blocks & Splat Expressions Explained

Today’s learning in #30DaysOfAWSTerraform was a complete game-changer in how I think about writing Terraform code. I explored three powerful expression techniques that transform Terraform from repetitive configuration to clean, automated, and scalable Infrastructure-as-Code:

1. Conditional Expressions
2. Dynamic Blocks
3. Splat Expressions

These expressions solve real-world DevOps challenges—avoiding repetitive blocks of code, enabling smart decision-making, and extracting values from multiple resources effortlessly.


Why Terraform Expressions Matter

Many beginners (including me earlier) start by hardcoding values and copy-pasting blocks repeatedly.
This quickly creates problems:

  • Manual edits = mistakes

  • Huge files = hard to read & maintain

  • Different environments = inconsistent results

Expressions fix all of this by making Terraform:

  • More concise

  • More efficient

  • More reusable

  • More scalable for Dev, Staging & Production


1. Conditional Expressions

Used when selecting values based on conditions (similar to if-else).

Example

instance_type = var.environment == "dev" ? "t2.micro" : "t3.micro"

How it works

  • If environment is dev → use t2.micro

  • Else → use t3.micro

Best practice
Store environment input in variables.tf and use conditionals inside resources—not hardcoded values.


2. Dynamic Blocks

Used to dynamically generate nested repeatable resource blocks—especially useful in security groups, IAM policies, scaling rules, etc.

Variable definition

variable "ingress_rule_dynamic_block" {
  description = "List of ingress rules for testing dynamic block expression"
  type = list(object({
    from_port   = number
    to_port     = number
    protocol    = string
    cidr_blocks = list(string)
    description = string
  }))
  default = [
    {
      from_port   = 80
      to_port     = 80
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
      description = "Allow HTTP traffic"
    },
    {
      from_port   = 443
      to_port     = 443
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
      description = "Allow HTTPS traffic"
    }
  ]
}

Dynamic block usage

  dynamic "ingress" {
    for_each = var.ingress_rule_dynamic_block
    content {
      from_port   = ingress_rule_dynamic_block.value.from_port
      to_port     = ingress_rule_dynamic_block.value.to_port
      protocol    = ingress_rule_dynamic_block.value.protocol
      cidr_blocks = ingress_rule_dynamic_block.value.cidr_blocks
      description = ingress_rule_dynamic_block.value.description
    }
  }

Benefits
✔ No duplicate nested blocks
✔ Easy to add/remove rules
✔ Cleaner code & great for large infra


3. Splat Expressions

Used to collect attributes from multiple resource instances.

Example

aws_instance.example[*].id

Local value

locals {
  all_instance_ids = aws_instance_conditional_instance[*].id
}

Output

output "all_instance_ids" {
  value = local.all_instance_ids
}

Use case

  • Collect ARNs, IDs, IPs of EC2 instances created with count or for_each


Pre-Apply Checklist

CheckWhy
terraform initconfigure backend & plugins
Validate variable typesavoid type mismatch errors
Balance braces & block syntaxcommon source of plan failures
Use quotes for CIDR"0.0.0.0/0"

Use splat expressions for count resources

avoid index errors

Final Thoughts

Today’s session helped me write cleaner, more professional Terraform code by removing repetition, centralizing logic, and improving scalability. Conditional expressions, dynamic blocks, and splat expressions are essential tools for building reusable Terraform modules and enterprise-grade AWS infrastructure.


Let’s Connect

Here is my repo link for detailed code lines: https://github.com/Mo-Adnan-Mo-Ayyub/Aws-with-Terraform

Here is the session link: 




Always open for collaboration, DevOps discussions, and CI/CD & Cloud architecture learning exchanges!

#DevOps #Terraform #AWS #InfrastructureAsCode #CloudEngineering #SRE #30DaysOfAWSTerraform #PiyushSachdeva #HCL #Automation #IaC

Comments

Popular posts from this blog

AWS with Terraform (Day 01)

AWS with Terraform (Day 02)

AWS with Terraform (Day 06)