Back to articles
Mastering Loops and Conditionals in Terraform
How-ToDevOps

Mastering Loops and Conditionals in Terraform

via Dev.to BeginnersMukami

Day 10 of the 30-Day Terraform Challenge — and today I learned that Terraform is actually a programming language in disguise. For the past 9 days, I've been writing infrastructure like it's 2010: one resource at a time, copy-pasting blocks, and feeling clever when I only duplicated code three times instead of five. Today I discovered loops and conditionals. And I'm never going back. The Problem: Copy-Paste Engineering Let me show you what I was doing before today: # Creating 3 users? Copy-paste! resource "aws_iam_user" "alice" { name = "alice" } resource "aws_iam_user" "bob" { name = "bob" } resource "aws_iam_user" "charlie" { name = "charlie" } # Security group with 3 rules? Copy-paste! ingress { from_port = 80 } ingress { from_port = 443 } ingress { from_port = 22 } This works. Until you need 10 users. Or 50 security group rules. Or you need to change something across all of them. There had to be a better way. There is. Tool 1: count — The Simple Loop count creates multiple copies of

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
5 views

Related Articles