Skip to content

nahorfelix/terraform-challenge-day10

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Day 10 — Terraform loops & conditionals

30-Day Terraform Challenge · March 2026

I spent this day on count, for_each, for expressions, and conditionals—then refactored my webserver cluster module so repeated blocks disappear and optional pieces (autoscaling) actually stay out of the graph when they’re off.


What I shipped

Path Notes
modules/services/webserver-cluster/ for_each on security group rules and on scaling policy / alarm maps; locals for instance size by environment; outputs built with for
live/dev/services/webserver-cluster/ Dev stack, autoscaling enabled
live/production/services/webserver-cluster/ Production stack, autoscaling disabled, larger default instance type
labs/iam-count-list/ IAM users driven by count + length(list) — shows why list order bites
labs/iam-for-each/ Same idea with for_each on toset(list) plus a tagged user map

VS Code setup I use

  • Folder: open terraform-challenge-day10 as the workspace root so paths in live/ and modules/ resolve cleanly.
  • Terminal: integrated terminal (Ctrl+`) — PowerShell on Windows is fine; I run all terraform commands from the specific stack directory.
  • Extensions: HashiCorp Terraform (syntax, terraform validate integration), optional AWS Toolkit if I’m jumping to the console.
  • Format: from repo root, terraform fmt -recursive before commits.

Running the webserver stacks

Dev (us-east-1, autoscaling on):

cd C:\Users\felix\terraform-challenge-day10\live\dev\services\webserver-cluster
terraform init
terraform validate
terraform plan

Flip enable_autoscaling to false in that main.tf, run terraform plan again, and the scaling policies / CPU alarms drop out—that’s the conditional map pattern in locals.tf doing its job.

Production:

cd C:\Users\felix\terraform-challenge-day10\live\production\services\webserver-cluster
terraform init
terraform validate
terraform plan

environment = "production" drives t3.small when instance_type is left empty; dev stays on t3.micro.


IAM scratch stacks (labs/)

Small, isolated roots—terraform destroy when finished so IAM users don’t linger.

count + ordered list (labs/iam-count-list/):

cd C:\Users\felix\terraform-challenge-day10\labs\iam-count-list
terraform init
terraform apply

Remove a middle element from user_names, plan again, and watch indices shift—that’s the behaviour I’m documenting in my write-up.

for_each + set / map (labs/iam-for-each/):

cd C:\Users\felix\terraform-challenge-day10\labs\iam-for-each
terraform init
terraform apply
terraform output principal_arns
terraform destroy

Stack

  • Terraform >= 1.0
  • AWS provider ~> 5.0
  • AWS credentials on the machine (env vars or shared config)

About

Day 10: Terraform loops and conditionals (count, for_each, for, locals) — 30-Day Challenge

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors