Bird
Raised Fist0
Terraformcloud~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does a for expression in Terraform primarily do?
easy
A. Creates a new resource in the cloud
B. Transforms each item in a list or map into a new list or map
C. Deletes resources based on a condition
D. Runs a shell command on the local machine

Solution

  1. Step 1: Understand the purpose of for expressions

    For expressions in Terraform are used to transform collections like lists or maps into new collections by applying a transformation to each item.
  2. Step 2: Differentiate from other Terraform actions

    Creating or deleting resources and running shell commands are not done by for expressions; they are handled by resource blocks or provisioners.
  3. Final Answer:

    Transforms each item in a list or map into a new list or map -> Option B
  4. Quick Check:

    For expressions = transform collections [OK]
Hint: For expressions change each item in a list/map to a new form [OK]
Common Mistakes:
  • Thinking for expressions create or delete resources
  • Confusing for expressions with provisioners
  • Assuming for expressions run commands
2. Which of the following is the correct syntax for a simple for expression transforming a list [1, 2, 3] to their squares in Terraform?
easy
A. [for n in [1, 2, 3] : n * n]
B. [for n : n * n in [1, 2, 3]]
C. [for n in [1, 2, 3] n * n]
D. [for n in [1, 2, 3] => n * n]

Solution

  1. Step 1: Recall Terraform for expression syntax

    The correct syntax is: [for <variable> in <list> : <expression>]. [for n in [1, 2, 3] : n * n] matches this exactly.
  2. Step 2: Check other options for syntax errors

    [for n : n * n in [1, 2, 3]] has wrong order, [for n in [1, 2, 3] n * n] misses colon, [for n in [1, 2, 3] => n * n] uses => which is invalid in Terraform for expressions.
  3. Final Answer:

    [for n in [1, 2, 3] : n * n] -> Option A
  4. Quick Check:

    Correct syntax = [for var in list : expr] [OK]
Hint: Remember syntax: for var in list : expression [OK]
Common Mistakes:
  • Omitting the colon ':' after the list variable
  • Using arrow '=>' instead of colon
  • Placing expression before 'in' keyword
3. Given the Terraform expression:
[for s in ["apple", "banana", "cherry"] : upper(s) if length(s) > 5]

What is the resulting list?
medium
A. ["banana", "cherry"]
B. ["APPLE", "BANANA", "CHERRY"]
C. ["BANANA", "CHERRY"]
D. ["APPLE"]

Solution

  1. Step 1: Identify filtering condition

    The expression filters strings with length greater than 5. "banana" (6) and "cherry" (6) qualify; "apple" (5) does not.
  2. Step 2: Apply transformation function

    For qualifying strings, upper(s) converts them to uppercase: "BANANA", "CHERRY".
  3. Final Answer:

    ["BANANA", "CHERRY"] -> Option C
  4. Quick Check:

    Filter length > 5 + uppercase = ["BANANA", "CHERRY"] [OK]
Hint: Filter first, then transform inside for expression [OK]
Common Mistakes:
  • Including items that don't meet the if condition
  • Not applying the upper() function
  • Confusing syntax order of filter and expression
4. Identify the error in this Terraform for expression:
[for k, v in {a=1, b=2} : "${k}=${v}" if v > 1]
medium
A. Using if condition outside the for expression
B. Missing colon after the map
C. Incorrect use of string interpolation syntax
D. Using two variables without parentheses in for expression

Solution

  1. Step 1: Check syntax for multiple variables in for expressions

    When using two variables (key and value) in a for expression, parentheses are required: for (k, v) in map.
  2. Step 2: Identify the error in given expression

    The expression uses 'for k, v in' without parentheses, causing syntax error.
  3. Final Answer:

    Using two variables without parentheses in for expression -> Option D
  4. Quick Check:

    Two variables need parentheses: for (k, v) in map [OK]
Hint: Use parentheses for multiple variables in for expressions [OK]
Common Mistakes:
  • Omitting parentheses around multiple variables
  • Placing if condition incorrectly
  • Misusing string interpolation syntax
5. You have a map of instance names to their CPU counts:
{"web"=2, "db"=4, "cache"=1}

Write a Terraform for expression that creates a list of instance names with CPU count greater than 1, all in uppercase.
hard
A. [for (name, cpu) in {"web"=2, "db"=4, "cache"=1} : upper(name) if cpu > 1]
B. [for name, cpu in {"web"=2, "db"=4, "cache"=1} : upper(name) if cpu > 1]
C. [for (name, cpu) in {"web"=2, "db"=4, "cache"=1} : name if cpu > 1]
D. [for (name, cpu) in {"web"=2, "db"=4, "cache"=1} : upper(cpu) if cpu > 1]

Solution

  1. Step 1: Use parentheses for key-value iteration

    When iterating over a map with two variables, use parentheses: for (name, cpu) in map.
  2. Step 2: Apply filter and transformation

    Filter with if cpu > 1 to select "web" and "db". Transform name to uppercase with upper(name).
  3. Final Answer:

    [for (name, cpu) in {"web"=2, "db"=4, "cache"=1} : upper(name) if cpu > 1] -> Option A
  4. Quick Check:

    Parentheses + filter + upper(name) = [for (name, cpu) in {"web"=2, "db"=4, "cache"=1} : upper(name) if cpu > 1] [OK]
Hint: Parentheses for map keys, filter with if, transform with upper() [OK]
Common Mistakes:
  • Omitting parentheses for key-value pairs
  • Transforming cpu instead of name
  • Not filtering correctly with if condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard