Recall & Review
beginner
What is a 'for expression' in Terraform?
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: <br>
[for n in var.numbers : n * 2]<br>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.<br>A map for expression returns key-value pairs using the syntax:<br>
{ for k, v in var.map : k => v }<br>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:<br>
[for n in var.numbers : n if n > 5]<br>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? <br>
[for s in ["a", "b", "c"] : upper(s)]✗ 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?
✗ 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?
✗ Incorrect
The 'if' condition after the colon filters the list to include only even numbers.
What type of collection does this expression produce? <br>
{ for k, v in var.map : k => v * 2 }✗ Incorrect
Curly braces with 'key => value' syntax produce a map; values are doubled here.
Can you use for expressions inside resource blocks in Terraform?
✗ 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.