0
0
Terraformcloud~10 mins

For expressions for transformation in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - For expressions for transformation
Start with input list/map
Apply for expression
Transform each item
Collect transformed items
Output new list/map
For expressions take each item from a list or map, change it, and make a new list or map with the changed items.
Execution Sample
Terraform
locals {
  names = ["alice", "bob", "carol"]
  upper_names = [for n in local.names : upper(n)]
}
This code takes a list of names and creates a new list with all names in uppercase.
Process Table
StepInput Item (n)TransformationOutput ItemOutput List State
1"alice"upper("alice")"ALICE"["ALICE"]
2"bob"upper("bob")"BOB"["ALICE", "BOB"]
3"carol"upper("carol")"CAROL"["ALICE", "BOB", "CAROL"]
4No more itemsEnd loopFinal list["ALICE", "BOB", "CAROL"]
💡 All items processed, for expression completes with transformed list.
Status Tracker
VariableStartAfter 1After 2After 3Final
nnone"alice""bob""carol"none
upper_names[]["ALICE"]["ALICE", "BOB"]["ALICE", "BOB", "CAROL"]["ALICE", "BOB", "CAROL"]
Key Moments - 3 Insights
Why does the output list grow after each step?
Because each input item is transformed and added to the output list, as shown in execution_table rows 1 to 3.
What happens if the input list is empty?
The for expression produces an empty list immediately, no iterations happen, similar to execution_table row 4 but with no items.
Can the transformation be more complex than a simple function call?
Yes, any expression can be used to transform each item, like conditionals or combining values, as long as it produces a valid output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output list after step 2?
A["ALICE"]
B["ALICE", "BOB"]
C["BOB"]
D["ALICE", "BOB", "CAROL"]
💡 Hint
Check the 'Output List State' column in execution_table row 2.
At which step does the for expression finish processing all items?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look for the row where 'No more items' is the input in execution_table.
If the input list had 4 names instead of 3, how many rows would the execution_table have before the exit row?
A4
B5
C3
D6
💡 Hint
Each input item gets one step, so count input items in execution_table rows.
Concept Snapshot
For expressions transform each item in a list or map.
Syntax: [for item in list : expression]
Produces a new list with transformed items.
Useful for changing or filtering data simply.
Runs once per input item, collects results.
Always returns a new list or map.
Full Transcript
For expressions in Terraform let you take each item from a list or map and change it to make a new list or map. For example, you can take a list of names and make a new list with all names uppercase. The process starts with the input list, then for each item, it applies a transformation, and adds the result to a new list. This repeats until all items are processed. The output is a new list with all transformed items. Beginners often wonder why the output list grows step by step, or what happens if the input is empty. The output list grows because each transformed item is added in order. If the input is empty, the output is empty too. You can use any expression to transform items, not just simple functions. The execution table shows each step clearly, tracking input, transformation, and output list state. This helps understand how the for expression works internally.