A 'for expression' in Terraform is a way to transform or create new collections by looping over existing ones, similar to a simple loop that builds a new list or map.
Click to reveal answer
beginner
How do you create a new list by doubling each number in an existing list using a for expression?
You write: [for n in var.numbers : n * 2] This loops over each number 'n' in 'var.numbers' and multiplies it by 2, creating a new list.
Click to reveal answer
intermediate
What is the difference between a for expression that creates a list and one that creates a map?
A list for expression returns a list of values. A map for expression returns key-value pairs using the syntax: { for k, v in var.map : k => v } This creates a new map by transforming keys and values.
Click to reveal answer
intermediate
How can you filter elements in a for expression?
You add an 'if' condition after the colon: [for n in var.numbers : n if n > 5] This creates a list of numbers greater than 5 only.
Click to reveal answer
beginner
Why use for expressions in Terraform configurations?
For expressions help you write less repetitive code by transforming data collections dynamically, making your infrastructure code cleaner and easier to maintain.
Click to reveal answer
What does this Terraform expression produce? [for s in ["a", "b", "c"] : upper(s)]
AError, 'upper' is not valid
B["A", "B", "C"]
C{"a" => "A", "b" => "B", "c" => "C"}
D["a", "b", "c"]
✗ Incorrect
The expression loops over each string and applies 'upper' to convert to uppercase, producing a list of uppercase letters.
How do you write a for expression that creates a map from a list of strings where keys are strings and values are their lengths?
A[ for s in var.strings : length(s) ]
B{ for s in var.strings : length(s) => s }
C{ for s in var.strings : s => length(s) }
D[ for s in var.strings : s ]
✗ Incorrect
Using curly braces and 'key => value' syntax creates a map where each string maps to its length.
Which of these filters elements in a list to only include even numbers?
A[for n in var.numbers : n if n % 2 == 0]
B[for n in var.numbers : n]
C{for n in var.numbers : n => n if n % 2 == 0}
D[for n in var.numbers : n if n > 0]
✗ Incorrect
The 'if' condition after the colon filters the list to include only even numbers.
What type of collection does this expression produce? { for k, v in var.map : k => v * 2 }
AAn error
BA list of doubled values
CA list of keys
DA map with values doubled
✗ Incorrect
Curly braces with 'key => value' syntax produce a map; values are doubled here.
Can you use for expressions inside resource blocks in Terraform?
AYes, to generate multiple resources or attributes dynamically
BNo, for expressions are only for variables
COnly in provider blocks
DOnly in output blocks
✗ Incorrect
For expressions can be used anywhere in Terraform configuration to create dynamic values.
Explain how to use a for expression to transform a list of strings into a list of their uppercase versions.
Think about looping over each item and changing it.
You got /4 concepts.
Describe how to filter elements in a list using a for expression in Terraform.
Filtering happens after the colon with an 'if' statement.
You got /4 concepts.
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
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.
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.
Final Answer:
Transforms each item in a list or map into a new list or map -> Option B
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
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.
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.
Final Answer:
[for n in [1, 2, 3] : n * n] -> Option A
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
Step 1: Identify filtering condition
The expression filters strings with length greater than 5. "banana" (6) and "cherry" (6) qualify; "apple" (5) does not.
Step 2: Apply transformation function
For qualifying strings, upper(s) converts them to uppercase: "BANANA", "CHERRY".